SQLite是一款輕量級(jí)的關(guān)系型數(shù)據(jù)庫(kù)。無(wú)需服務(wù)進(jìn)程谒臼,無(wú)需安裝和配置朝刊,支持事務(wù)處理。它的運(yùn)算速度快蜈缤,占用資源少拾氓,適合在存儲(chǔ)大量復(fù)雜的關(guān)系型數(shù)據(jù)的時(shí)候使用。
一底哥、SQLite數(shù)據(jù)庫(kù)使用
(1)使用幫助類(lèi)SQLiteOpenHelper創(chuàng)建數(shù)據(jù)庫(kù)
- SQLiteOpenHelper是SQLiteDatabase的幫助類(lèi)咙鞍, 用于管理數(shù)據(jù)庫(kù)的創(chuàng)建和升級(jí)。
- 自定義幫助類(lèi)繼承SQLiteOpenHelper趾徽,并重寫(xiě)onCreate()和 onUpgrade()续滋,分別在這兩個(gè)方法中去實(shí)現(xiàn)創(chuàng)建、升級(jí)數(shù)據(jù)庫(kù)的邏輯孵奶。
- 調(diào)用幫助類(lèi)對(duì)象的getReadableDatabase() 和getWritableDatabase()去創(chuàng)建或打開(kāi)一個(gè)現(xiàn)有的數(shù)據(jù)庫(kù)疲酌,并返回一個(gè)可對(duì)數(shù)據(jù)庫(kù)操作的SQLiteDatabase。
- 在onCreate方法中調(diào)用SQLiteDatabase的方法execSQL()創(chuàng)建數(shù)據(jù)庫(kù)。execSQL()可以接受和處理SQL語(yǔ)句朗恳。
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String CREATE_MEMBER = "create table member(id integer primary key autoincrement, name text, sex text, age integer)";
public static final String CREATE_INTEREST = "create table interest(id integer primary key autoincrement,content text)";
public static final String CREATE_MEMBER_AGE = "alter table member add column age integer";
public MySQLiteHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//創(chuàng)建member表
db.execSQL(CREATE_MEMBER);
//創(chuàng)建interest表
db.execSQL(CREATE_INTEREST);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
//升級(jí)數(shù)據(jù)庫(kù)增加表
db.execSQL(CREATE_INTEREST);
case 2:
//升級(jí)數(shù)據(jù)庫(kù)增加表中字段age
db.execSQL(CREATE_MEMBER_AGE);
break;
default:
}
}
}
(2)操作數(shù)據(jù)表
//獲取到SQLiteDatabase的實(shí)例
SQLiteOpenHelper dbHelper = new MySQLiteHelper(this, "demo.db", null, 1);
private void insert() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "黃渤");
values.put("sex", "男");
values.put("age", 34);
db.insert("member", null, values);
values.clear();
db.execSQL("insert into member(name,sex,age) values('黃渤','男',34)");
}
private void update() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "岳云鵬");
db.update("member", values, "id = ?", new String[]{"1"});
values.clear();
db.execSQL("update member SET id = '1' WHERE name = '岳云鵬'");
}
private void delete() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("member", "name= ?", new String[]{"岳云鵬"});
db.execSQL("delete from member where name = '岳云鵬'");
}
@SuppressLint("Recycle")
private void rawQuery() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
//查詢member表中所有數(shù)據(jù)
Cursor cursor1 = db.query("member", null, null, null, null, null, null);
//查詢member表中年齡大于30的數(shù)據(jù)
Cursor cursor2 = db.query("member", null, "age > ?", new String[]{"30"}, null, null, null);
Cursor cursor3 = db.rawQuery("select * from member", null);
//從Cursor對(duì)象中取出數(shù)據(jù)
List<Member> memberList = new ArrayList<Member>();
if (cursor1 != null && cursor1.moveToFirst()) {
do {
int id = cursor1.getInt(cursor1.getColumnIndex("id"));
String name= cursor1.getString(cursor1.getColumnIndex("name"));
String sex= cursor1.getString(cursor1.getColumnIndex("sex"));
int age= cursor1.getInt(cursor1.getColumnIndex("age"));
Member member = new Member();
member.setId(id);
member.setName(name);
member.setSex(sex);
member.setAge(age);
memberList.add(member);
} while (cursor1.moveToNext());
}
}