Android SQLite 学习4

Ref: http://blog.csdn.net/u010105970/article/details/51123265?locationNum=12&fps=1

这个例子比较简洁,适合新手入门

MainActivity.java

package com.example.databasedemo;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.idescout.sql.SqlScoutServer;

public class MainActivity extends Activity {
    //用于创建帮助器对象
    private MyOpenHelper oh;
    //用于创建数据库对象
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SqlScoutServer.create(this, getPackageName());
    }

    //创建数据库
    public void createDatabase(View view) {
        //创建帮助器对象
        oh = new MyOpenHelper(this, "people.db", null, 1);
        //创建数据库对象
        db = oh.getWritableDatabase();
    }

    //向数据库中添加数据
    public void Insert(View view) {
        //向学生表中添加10名学生
        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"刘得意", 19, 1001, 60, 98, 75});
        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"王锐", 20, 1002, 63, 90, 96});
        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"何煜中", 19, 1003, 90, 73, 82});
        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"王磊", 21, 1004, 87, 86, 92});
        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"冯松", 19, 1005, 89, 98, 83});
        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"裴培", 20, 1006, 75, 82, 91});
        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"马骁", 19, 1007, 62, 67, 90});
        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"马婧", 20, 1008, 98, 84, 87});
        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"周俊升", 19, 1009, 57, 68, 96});
        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"贺祺", 21, 1010, 61, 96, 72});
    }

    //删除数据库中的数据
    public void Delete(View view) {
        //删除姓名为"刘得意"的学生的信息
        db.execSQL("delete from Student where name = ?", new Object[]{"刘得意"});
    }

    //修改数据库中的数据
    public void Update(View view) {
        //将数据库中所有人的学号减少1
        db.execSQL("update student set no = no -1");
    }

    //查询数据库中的数据
    public void Select(View view) {
        //查询数据库中学生的姓名和以其对应的C++成绩,返回值为一个结果集
        Cursor cursor = db.rawQuery("select name, cpp from student", null);

        while (cursor.moveToNext()) {
            //cursor.getColumnIndex("name")获得姓名所在的列
            String name = cursor.getString(cursor.getColumnIndex("name"));
            float cpp = cursor.getFloat(cursor.getColumnIndex("cpp"));
            //输出学生的姓名和与姓名对应的C++成绩
            Log.d("MainActivity", '[' + name + ", " + cpp + ']');
        }
    }
}
View Code
MyOpenHelper.java
package com.example.databasedemo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

//创建一个抽象类SQLiteOpenHelper的实现类MyOpenHelper
public class MyOpenHelper extends SQLiteOpenHelper {
    /**
     * MyOpenHelper构造方法
     * @param context 上下文
     * @param name 数据库文件的名字
     * @param factory 游标工厂(结果集)
     * @param version 数据库的版本号(用于升级)
     */
    public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    //创建数据库时,调用此方法
    @Override
    public void onCreate(SQLiteDatabase db) {

        Log.d("MainActivity", "数据库创建成功");

        //创建一个学生表
        db.execSQL("create table student(_id integer primary key autoincrement, name char(10), age integer, no integer, cpp float, math float, english float)");
    }

    //数据库升级时调用此方法
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        Log.d("MainActivity", "数据库升级成功");
    }
}
View Code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="createDatabase"
        android:textSize="30dp"
        android:onClick="createDatabase"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Insert"
        android:textSize="30dp"
        android:onClick="Insert"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Delete"
        android:textSize="30dp"
        android:onClick="Delete"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Update"
        android:textSize="30dp"
        android:onClick="Update"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select"
        android:textSize="30dp"
        android:onClick="Select"/>

</LinearLayout>
View Code

 


GitHub id HBU

Android Debug Database

https://github.com/amitshekhariitbhu/Android-Debug-Database

把Android-Debug-Database引入自己的项目

http://ask.android-studio.org/?/question/437

 

使用说明

http://www.cnblogs.com/ifly_326/p/6509656.html

【注意事项】

  1. Android-Debug-Database 在使用中,一定要检查好在一个局域网 (家里两个WiFi,忽略了,导致开始网址总是 http://0.0.0.0:8080)
  2. 浏览器最好用Google的chrome,打开速度明显快,毕竟Android是Google的。。。
  3. 目前仅在手机上测试成功,模拟器上还没测试出来,一用adb就头疼。。。

模拟器试验成功:先执行adb forward tcp:8080 tcp:8080 (cmd环境),再打开http://localhost:8080(Chrome浏览器) 2017.6.20

可以编辑数据,很好很强大


https://juejin.im/post/58e0d781a0bb9f0069ec08d3

SQLScout 挺好用,可惜是收费软件,50美金,囊中羞涩。。。

https://www.idescout.com/secure/buy

posted on 2017-06-18 22:39  HBU_DAVID  阅读(263)  评论(0编辑  收藏  举报

导航