Android中SQLite的使用

  • 在MainActivity中创建数据库:
        //创建新的数据库
        //只有当数据库调用getWritableDatabase()或getReadableDatabase()方法时,
        //才真正的创建数据库并调用onCreate(),onUpgrand()函数进行数据库信息的写入
                DatabaseHelper databaseHelper = new DatabaseHelper(
                    MainActivity.this, "Database", null, 1);//Context,数据库名,游标类,数据库版本
        //得到可写的数据库,返回SQLiteDatabase对象,通过该对象对数据库进行读写操作
                SQLiteDatabase db = databaseHelper.getWritableDatabase();
  • DatabaseHelper类(继承自SQLiteOpenHelper类):
package learn.ida.database;

import android.accounts.Account;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

//DatabaseHelper类必须继承自其父类,并且必须有构造方法,onCreate(),onUpgrand()三个构造函数
//提供getWriteableDatabase()和getReadableDatabase()函数用于返回SQLiteDatabase对象,通过该对象对数据库进行读写操作或读取操作
//onCreate()和onUpgrade()函数用于创建和升级数据库使用
public class DatabaseHelper extends SQLiteOpenHelper {

    //DatabaseHelper必须要有的构造函数,进行数据库的创建
    public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        //通过super调用父类的构造函数进行数据库的创建
        super(context, name, null, version);

    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String SQLCreateTableCommand = "Create table User ( UID int );";
        db.execSQL(SQLCreateTableCommand);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion >= newVersion) {
            return;
        }
        String SQLUpgradeDatabaseCommand = "Drop table if exists User;";
        db.execSQL(SQLUpgradeDatabaseCommand);
        this.onCreate(db);
    }
}
  • 数据库DDL操作:

  • Insert:

            DatabaseHelper databaseHelper = new DatabaseHelper(MainActivity.this, "Database", null, 1);
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("UID", 1);//contentValues用于存入新值,键值对为列名,新值
            db.insert("User", null, contentValues);//表名,如果插入空值则置为null,新值
            db.close();//关闭数据库连接
    
  • Update:

            DatabaseHelper databaseHelper = new DatabaseHelper(MainActivity.this, "Database", null, 1);
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("UID", "2");
            db.update("User", contentValues, "UID=?", new String[]{"1"});//表名,新值,条件("列名=占位符"),条件具体值
            db.close();
    
  • Select:

            DatabaseHelper databaseHelper = new DatabaseHelper(MainActivity.this, "Database", null, 1);
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            //创建游标进行数据循环读取
            //表名,要查询的列,查询条件,条件具体值,groupBy,having,orderBy
            Cursor cursor = db.query("User", new String[]{"UID"}, "UID=?", new String[]{"2"}, null, null, null);
            while (cursor.moveToNext()) {//循环,使游标指向下一条记录
                String UID = cursor.getString(cursor.getColumnIndex("UID"));//获取游标序号列的值(游标所指向列的序列号)
                TextView textView = (TextView) findViewById(R.id.textView);
                textView.setText(UID);
            }
            db.close();
    
  • Delete:

            DatabaseHelper databaseHelper = new DatabaseHelper(MainActivity.this, "Database", null, 1);
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            db.delete("User", "UID = ?", new String[]{"2"});//表名,删除条件,条件的具体值
            db.close();
    
  • 通过adb工具查询数据库(要有设备连接):
    进入cmd –> adb shell –> cd /data/data –> cd –> cd /databases –> sqlite3 –> .schem(用于显示DDL语句)

你可能感兴趣的:(android,数据库,sqlite)