1. SQLiteOpenHelper -- 负责创建与打开数据库和版本管理
构造方法:
/**
* 在SQLiteOpenHelper的子类当中,必须有该构造函数
* @param context 上下文对象
* @param name 数据库名称
* @param factory CursorFactory对象factory用于查询时构造Cursor的子类对象并返回,或者传入null使用默认的factory构造
* @param version 当前数据库的版本,值必须是整数并且是递增的状态
*/
public DatabaseHelper(Context context, String name, CursorFactory factory,
int version) {
//必须通过super调用父类当中的构造函数 Create a helper object to create, open, and/or manage a database.
super(context, name, factory, version);
}
3个回调方法:onCreate(SQLiteDatabase)方法: 负责创建数据库(Called when the database is created for the first time.)
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE students (student_no text primary key, student_name text not null);";
db.execSQL(sql);
}
onUpgrade(SQLiteDatabase, int, int)负责更新数据库(Called when the database needs to be upgraded.)
如果发现版本号不一样,就会自动调用onUpgrade函数,让你在这里对数据库进行升级
onOpen(SQLiteDatabase)打开数据库(Called when the database has been opened.)
获取操作数据库的对象:
getReadableDatabase() (Create and/or open a database.)
getWritableDatabase() (Create and/or open a database that will be used for reading and writing.) 返回的是SQLiteDataBase的对象。
2. SQLiteDataBase 管理数据库,提供添加、更新、删除、检索、执行SQL指令和其他管理数据库的功能。在同一个应用程序里数据库名称必须是唯一的。
1).添加(Insert)一行数据到数据库
insert (String table, String nullColumnHack, ContentValues values)
table:插入的表格名称.(the table to insert the row into)
nullColumnHack:optional:(may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty)
values:添加的数据项.(this map contains the initial column values for the row. The keys should be the column names and the values the column values)
2).更新(update)
update (String table, ContentValues values, String whereClause, String[] whereArgs)
values:更新的数据项(a map from column names to new column values. null is a valid value that will be translated to NULL)
whereClause:查询条件(the optional WHERE clause to apply when updating. Passing null will update all rows)
whereArgs:定义SQL WHERE语句的相关查询参数.
3).删除(delete)
delete (String table, String whereClause, String[] whereArgs)
whereClause:(the optional WHERE clause to apply when deleting. Passing null will delete all rows.)
4).检索(query)
query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
table:数据表名称
columns:检索后的返回字段项目,设置为null表示返回所有字段项目,不建议使用null,防止
selection:SQL WHERE语句设置查询的条件,null表示所有行都要返回,可以为null
selectionArgs:定义SQL WHERE语句的相关查询参数,替换语句中的"?",可以为null
groupBy:设置分组的条件,可以为null
having:设置指定的分组,可以为null
orderBy:设置排序的条件,可以为null
limit:设置返回数量的限制,可以为null
返回值是Cursor对象,指向返回数据项的第一行。
ex:
class DBConnection extends SQLiteOpenHelper
helper = new DBConnection(this);
ContentValues values =...
values.put...
SQLiteDatabase db = helper.getReadableDatabase();
db.insert(UserSchema.TABLE_NAME, null, values);
String where = UserSchema.ID + " = " + id_this;
db.update(UserSchema.TABLE_NAME, values, where ,null);
db.delete(UserSchema.TABLE_NAME, where ,null);
Cursor c = db.query(UserSchema.TABLE_NAME, new String[] {UserSchema.USER_NAME}, null, null, null, null, null);
db.close();