SQlite使用

Posted 丛林小阁楼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQlite使用相关的知识,希望对你有一定的参考价值。

SQlite是android默认的数据库,属于轻量型的关系型数据库。用法如下:

DBHelper类:

public class MyDBHelper extends SQLiteOpenHelper{

    public static final String DB_NAME = "dbtest1.db";
    public static final String TABLE_NAME = "table1";

    public MyDBHelper(Context context) {
        super(context, DB_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql0= "drop table if exists " + TABLE_NAME;
        db.execSQL(sql0);

        String sql = "create table if not exists " + TABLE_NAME + " (id integer primary key, name text, age integer, address text)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "drop table if exists " + TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
    }
}

具体使用方式:

public class DbTest {

    private static MyDBHelper dbHelper = null;

    private static void createTable(Context context){
        if(dbHelper == null){
            dbHelper = new MyDBHelper(context);
        }
    }

    private static void insertTest(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.beginTransaction();
        db.execSQL("insert into " + MyDBHelper.TABLE_NAME + " (id, name, age, address) values(1, ‘Jack‘, 25, ‘beijing‘ )");
        db.execSQL("insert into " + MyDBHelper.TABLE_NAME + " (id, name, age, address) values(2, ‘Green‘, 26, ‘shanghai‘ )");
        db.execSQL("insert into " + MyDBHelper.TABLE_NAME + " (id, name, age, address) values(3, ‘Herry‘, 27, ‘nanjing‘ )");
//        db.setTransactionSuccessful();

//        db.beginTransaction();
        ContentValues values = new ContentValues();
        values.put("id", 4);
        values.put("name", "Mary");
        values.put("age", 28);
        values.put("address", "shenzhen");
        db.insertOrThrow(MyDBHelper.TABLE_NAME, null, values);
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    private static void queryTest(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String[] colums = new String[]{"id", "name", "age", "address"};
        Cursor cursor = db.query(MyDBHelper.TABLE_NAME, colums, null, null, null, null, null);
        if(cursor!=null && cursor.getCount()>0){
            while(cursor.moveToNext()){
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                int age = cursor.getInt(cursor.getColumnIndex("age"));
                String address = cursor.getString(cursor.getColumnIndex("address"));
                Log.i(MainActivity.TAG, "(" + id + ", " + name + ", " + age + ", " + address + ")");
            }
        }
        cursor.close();
    }

    private static void deleteTest(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
//        String sql = "delete from " + MyDBHelper.TABLE_NAME + " where name = ‘Green‘";
//        db.execSQL(sql);

        db.beginTransaction();
        db.delete(MyDBHelper.TABLE_NAME, "name=?", new String[]{"Green"});
        db.setTransactionSuccessful();
//        db.endTransaction();
    }

    private static void updateTest(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
//        String sql = "update " + MyDBHelper.TABLE_NAME + " set name=‘change‘ where age=27";
//        db.execSQL(sql);

        db.beginTransaction();
        ContentValues values = new ContentValues();
        values.put("name", "change1");
        db.update(MyDBHelper.TABLE_NAME, values, "age=?", new String[]{"27"});
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    public static void dbTest(Context context){
        createTable(context);
        insertTest();
        queryTest();

//        deleteTest();
//        queryTest();

        updateTest();
        queryTest();
    }

}

 

以上是关于SQlite使用的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向使用 DB Browser 查看并修改 SQLite 数据库 ( 从 Android 应用数据目录中拷贝数据库文件 | 使用 DB Browser 工具查看数据块文件 )(代码片段

SQLite 片段函数实现不会在 TextView 中将文本格式化为 HTML

如何在片段中访问 SQLite 数据到 ListView

java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.database.sqlite异常(代码片段

如何使用光标和循环显示来自 sqlite 的片段的 recyclerview

如何使用列表视图在选项卡式活动的片段中呈现 SQLite 表数据 [重复]