1、SQLiteOpenHelper类的使用
SQLiteOpenHelper类封装了一些对管理数据库的操作,方便对数据库的创建和升级。
两个构造方法
一般使用参数少的,四个参数的方法
MyTestOpenHelper myTestOpenHelper = new MyTestOpenHelper(context, DB_NAME , null, VERSION);
第一个参数是context 第二个是数据库名称 第三个参数是允许我们查询时返回一个自定义的Cuisor,一般传入null;第四个参数是数据库版本号,用于升级数据库;
两个重要的回调方法
1)onCreate()用于创建数据库
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_CONTACT); //创建Contact
}
2)onUpdate()用于升级数据库
根据数据库版本号来升级数据库,当数据库版本好大于久数据库版本号时调用次方法。
两个重要的实例方法
1)SQLiteDatabase db = getReadableDatabase()以只读方式打开数据库
2)SQLiteDatabase db = getWriteableDatabase()以可写方式创建(调用onCreate()方法)或打开数据库
当磁盘空间已满时,getWriteableDatabase会抛出异常。
数据库的常用形式总结:
例:假设APP中需要使用一张Contact表,表如下:
表名:Contact
表内容及创建是SQL语句:id integer primary key autoincrement,
name text,
gender text,
age integer
在db包中创建自己的MySQLiteOpenHelper类继承SQLiteOpenHelper,以及MyDatabase类,在model中创建数据库里表的实体类Contact
MySQLiteOpenHelper类一般实现内容:
1) 定义创建各表的SQL语句
private static final String CREATE_CONTACT = "create table Contact ("
+ "id integer primary key autoincrement, "
+ "name integer,"
+ "gender text,"
+ "age integer)";
2)MySQLiteOpenHelper 实现SQLiteOpenHelper中的onCreate(SQLiteDatebase db)方法
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_CONTACT); //创建Contact
}
3)MySQLiteOpenHelper 实现SQLiteOpenHelper中的onUpdate(SQLiteDatabase ,int oldVersion, int newVersion)方法,首个版本不需要创建
4)重写构造方法
public MySQLiteDatabaseOpenHelper(Content content, String name, CursorFactory factory, int version)
{ super(content, name, factory, version);
mContent = content;
}
MyDatabase实体类便于以后对表的操作,一般定义各字段并生成个各自的set和get方法
MyDatabase类封装一系列数据库表的操作,提供同步实例方法,保存和读取单数据
同步实例方法:根据Singleton设计原则,将构造方法私有化后提供一个同步锁的实例方法,保证整个进程中同时只能有一个实例能操作数据库
public static final String DB_NAME = "mytest";
public static final int VERSION = 1;
private static MyTestDB myTestDB;
private SQLiteDatabase db;
private MyTestDB(Context context) {
MyTestOpenHelper myTestOpenHelper = new MyTestOpenHelper(context, DB_NAME , null, VERSION);
db = myTestOpenHelper.getWritableDatabase();
}
public synchronized static MyTestDB getInstance (Context context) {
if (myTestDB == null) {
myTestDB = new MyTestDB(context);
}
return myTestDB;
}
保存操作saveContact(Contact contact)使用SQLiteDatabase类中的insert()方法
void saveContact(Contact contact){
if (contact != null) {
Values values = new Values();
values.putInt(contact.getId());
values.putString(contact.getName());
values.putString(contact.getGender());
values.putInt(contact.getAge());
db.insert("Contact,null,values);
}
}
读取操作Contact loadContact()使用SQLiteDatabase类中的查询query()方法,会返回一个Cursor,然后遍历取出数据即可
List<Contact> loadContact(){
List<Contact> list = new ArrayList<>();
Cursor cursor = null;
cursor = db.query("Contact",null,null,null,null,null.null);
if (cursor.moveToFrist()) {
do {
Contact contact = new contact;
contact.setId(cursor.getInt(cursor.getColumnIndex("id")));
contact.setName(cursor.getString(cursor.getColumnIndex("name)));
contact.setGender(cursor.getString(cursor.getColumnIndex("gender")));
contact.setAge(cursor.getInt(cursor.getColumnIndex("age")));
list.add(contact);
} while(cursor.moveToNext());
}
return list;
}
此类中还可以封装一些其他数据的的操作,不一一介绍了。