SQLiteDatabase 是關於資料庫操作的 可用於 insert delete update query 等操作 可惜美中不足的是:
1. 不支援建立資料庫
2. 不支援版本更新
所以出現了SQLiteOpenHelper類,是一個抽象類別,一般繼承它
一般裡面要寫3個函數:
建構函式
oncreate()初始化的時候使用,一般用於建表
onupdate()升級時候使用,一般刪除現有表,然後在重建
然後使用SQLiteOpenHelper的類函數:getWritableDatabase()或者getReadableDatabase()擷取資料庫執行個體
然後進行query,insert,update,delete操作。
mDb.insert(TABLE_NAME, null, newValues);
newValues是一個類似bundle和hashtable的集合,儲存索引值對
mDb.delete(TABLE_NAME, ID + "=" + id, null);
return mDb.delete(TABLE_NAME, null, null);
public PersonInfo[] queryOneData(long id)
{
Cursor result = mDb.query(TABLE_NAME, new String[] { ID,
PERSONINFO_CODE, PERSONINFO_DISPLAYPICTURE,
PERSONINFO_NICKNAME, PERSONINFO_BIRTHDAY, PERSONINFO_GENDER,
PERSONINFO_SIGNATURE, PERSONINFO_SIGNATUREDATETIME,
PERSONINFO_PASSWORD, PERSONINFO_DISPLAYPICTUREBINARY,
PERSONINFO_REGISTERTIME, PERSONINFO_UPDATETIME },
ID + "=" + id, null, null, null, null);
PersonInfo[]pInfo=ConvertToPersonInfo(result);
if(result!=null&&!result.isClosed()){
result.close();
result=null;
}
return pInfo;
}
public long updateOneData(long id, PersonInfo personInfo)
{
ContentValues newValues = new ContentValues();
newValues.put(PERSONINFO_CODE, personInfo.getCode());
newValues
.put(PERSONINFO_DISPLAYPICTURE, personInfo.getDisplayPicture());
newValues.put(PERSONINFO_NICKNAME, personInfo.getNickname());
newValues.put(PERSONINFO_BIRTHDAY, personInfo.getBirthday());
newValues.put(PERSONINFO_GENDER, personInfo.getGender());
newValues.put(PERSONINFO_SIGNATURE, personInfo.getSignature());
newValues.put(PERSONINFO_SIGNATUREDATETIME, personInfo
.getSignatureDatatime());
newValues.put(PERSONINFO_PASSWORD, personInfo.getPassword());
newValues.put(PERSONINFO_DISPLAYPICTUREBINARY, "");
newValues.put(PERSONINFO_REGISTERTIME, personInfo.getRegisterTime());
newValues.put(PERSONINFO_UPDATETIME, personInfo.getUpdateTime());
return mDb.update(TABLE_NAME, newValues, ID + "=" + id, null);
}
public long insert(PersonInfo personInfo)
{
ContentValues newValues = new ContentValues();
newValues.put(PERSONINFO_CODE, personInfo.getCode());
newValues
.put(PERSONINFO_DISPLAYPICTURE, personInfo.getDisplayPicture());
newValues.put(PERSONINFO_NICKNAME, personInfo.getNickname());
newValues.put(PERSONINFO_BIRTHDAY, personInfo.getBirthday());
newValues.put(PERSONINFO_GENDER, personInfo.getGender());
newValues.put(PERSONINFO_SIGNATURE, personInfo.getSignature());
newValues.put(PERSONINFO_SIGNATUREDATETIME, personInfo
.getSignatureDatatime());
newValues.put(PERSONINFO_PASSWORD, personInfo.getPassword());
newValues.put(PERSONINFO_DISPLAYPICTUREBINARY, "");
newValues.put(PERSONINFO_REGISTERTIME, personInfo.getRegisterTime());
newValues.put(PERSONINFO_UPDATETIME, personInfo.getUpdateTime());
return mDb.insert(TABLE_NAME, null, newValues);
}
public long deleteOneData(long id)
{
return mDb.delete(TABLE_NAME, ID + "=" + id, null);
}