干货区安卓 完美收藏Android练级塔

十.Android 数据持久化四种方法之一——SQLite

2016-08-07  本文已影响1591人  KaelQ

1.概述

2.用法

方法 用处
onCreate() 第一次创建数据库时调用
onUpgrade() 数据库升级时调用
方法 用处
getReadleDatabase() 先以读写方式打开数据库,如果数据库不可写入,则以只读打开。
getWriterDatabase() 以读写方式打开数据库,如果数据库不可写入,则出现异常
参数 含义
table 表名
columns 列名
selection where约束条件
selectionArgs where占位符的具体指
groupBy groupBy约束条件
having 其他约束条件
orderBy 排序方式

将cursor获取返回的内容,使用cursor.getString(cursor.getColumnIndex("列名"))获取具体列的值。

参数 含义
table 表名
null 未指定添加数据下自动赋值null
ContentValues 数据组合,使用put(列名,值)加入。

3.例子

* 继承SQLiteOpenHelper类的StuDBhelper类

public class StuDBHelper extends SQLiteOpenHelper {
    Context mcontext;
    private static String createStuTable="create table student(" +
            "id integer primary key autoincrement," +
            "name varchar(16)," +
            "password varchar(16))";
    private static String createTeaTable="create table teacher(" +
            "id integer primary key autoincrement," +
            "name varchar(16)," +
            "password varchar(16))";
    public StuDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.mcontext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createStuTable);
        Toast.makeText(mcontext,"student数据库已创建",Toast.LENGTH_SHORT).show();
        db.execSQL(createTeaTable);
        Toast.makeText(mcontext,"teacher数据库已创建",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists student");
        db.execSQL("drop table if exists teacher");
        onCreate(db);
        Toast.makeText(mcontext,"数据库已更新",Toast.LENGTH_SHORT).show();
    }
}
public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_createdb:
                dbHelper = new StuDBHelper(this, "School.db", null, 1);
                db = dbHelper.getReadableDatabase();
                break;
            case R.id.btn_updatedb:
                dbHelper = new StuDBHelper(this, "School.db", null, 2);
                db = dbHelper.getReadableDatabase();
                break;
            case R.id.btn_deletedb:
                deleteDatabase("School.db");
                deleteDatabase("Teacher.db");
                Toast.makeText(this,"数据库已删除",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_query:
                editText.setText("");
                Result="";
                db = dbHelper.getReadableDatabase();
                Cursor cursor = db.query("student", null, null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        int id=cursor.getInt(cursor.getColumnIndex("id"));
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String password = cursor.getString(cursor.getColumnIndex("password"));
                        Result = Result +"id "+id+ "姓名 " + name + " " + "密码 " + password +"\n";
                    } while (cursor.moveToNext());
                }
                cursor.close();
                editText.setText(Result);
                break;
            case R.id.btn_insert:
                db = dbHelper.getReadableDatabase();
                ContentValues values = new ContentValues();
                values.put("name", "ming");
                values.put("password", "ming");
                db.insert("student", null, values);
                values.clear();
                values.put("name", "fang");
                values.put("password", "fang");
                db.insert("student", null, values);
                Toast.makeText(this,"数据已插入",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_delete:
                db = dbHelper.getReadableDatabase();
                db.delete("student", "name=?", new String[]{"ming"});
                Toast.makeText(this,"数据已删除",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_update:
                db = dbHelper.getReadableDatabase();
                ContentValues values1 = new ContentValues();
                values1.put("password", "hello");
                db.update("student", values1, "name=?", new String[]{"fang"});
                Toast.makeText(this,"数据已更新",Toast.LENGTH_SHORT).show();
                break;
        }
    }

4.日常福利

Github源码地址

上一篇下一篇

猜你喜欢

热点阅读