概述
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);
}
最后
以上就是飞快麦片为你收集整理的数据库操作总结的全部内容,希望文章能够帮你解决数据库操作总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复