Android SQLite数据库操作实例
Android的自带数据库SQLite小巧且功能强大,Android提供了两种方式去操作数据库,第一种是用SQL语句去操作数据,SQLite支持标准的SQL,其分页等操作与Mysql一样,以下是利用SQL操作SQLite:
Java代码
- importjava.util.ArrayList;
- importjava.util.List;
-
- importandroid.content.Context;
- importandroid.database.Cursor;
- importandroid.database.sqlite.SQLiteDatabase;
- publicclassPersonDaoClassic{
- privateDBOpenHelperhelper;
- publicPersonDaoClassic(Contextcontext){
- helper=newDBOpenHelper(context);
- }
- voidinsert(Personp){
-
- SQLiteDatabasedb=helper.getWritableDatabase();
- //执行SQL语句,替换占位符
- db.execSQL("INSERTINTOperson(name,balance)VALUES(?,?)",newObject[]{p.getName(),p.getBalance()});
- //释放资源
- db.close();
- voiddelete(intid){
- db.execSQL("DELETEFROMpersonWHEREid=?",85); font-weight:bold">newObject[]{id});
- voidupdate(Personp){
- db.execSQL("UPDATEpersonSETname=?,balance=?WHEREid=?",p.getBalance(),p.getId()});
- publicPersonquery( SQLiteDatabasedb=helper.getReadableDatabase();
- //执行原始查询,得到一个Cursor(类似ResultSet)
- Cursorc=db.rawQuery("SELECTname,balanceFROMpersonWHEREid=?",85); font-weight:bold">newString[]{String.valueOf(id)});
- Personp=null;
- //判断Cursor是否有下一条记录
- if(c.moveToNext())
- //从Cursor中获取数据,创建Person对象
- p=newPerson(id,c.getString(0),c.getInt(1));
- c.close();
- returnp;
- publicList<Person>queryAll(){
- Cursorc=db.rawQuery("SELECTid,name,balanceFROMperson",85); font-weight:bold">null);
- List<Person>persons=newArrayList<Person>();
- while(c.moveToNext())
- persons.add(newPerson(c.getInt(1),0)">2)));
- returnpersons;
- publicList<Person>queryPage(intpageNum,85); font-weight:bold">intcapacity){
- //开始索引
- Stringstart=String.valueOf((pageNum-1)*capacity);
- //查询的个数
- Stringlength=String.valueOf(capacity);
- //翻页查询语句,和MySQL中相同
- ,85); font-weight:bold">newString[]{start,length});
- intqueryCount(){
- //查询记录条数
- Cursorc=db.rawQuery("SELECTCOUNT(*)FROMperson",250); line-height:18px"> c.moveToNext();
- intcount=c.getInt(0);
- returncount;
- }
除上述方法以外,android还给我们带来了另外一种更加简单,也是android推荐使用的一种方式,此种方式把数据封装在ContentValues中,因为android编程过程中经常会使用到已经封装好了数据的ContentValues,所以使用第二种方式在有些时候更加便捷,以下是代码:
packagecn.itcast.sqlite;
importandroid.content.ContentValues;
classPersonDao{
publicPersonDao(Contextcontext){
voidremit(intfrom,85); font-weight:bold">intto,85); font-weight:bold">intamount){
//开启事务
db.beginTransaction();
try{
db.execSQL("UPDATEpersonSETbalance=balance-?WHEREid=?",85); font-weight:bold">newObject[]{amount,from});
db.execSQL("UPDATEpersonSETbalance=balance+?WHEREid=?",to});
db.setTransactionSuccessful();
}catch(Exceptione){
e.printStackTrace();
//结束事务,将事务成功点前面的代码提交
db.endTransaction();
//准备数据
ContentValuesvalues=newContentValues();
values.put("name",p.getName());
values.put("balance",p.getBalance());
//通过ContentValues中的数据拼接SQL语句,执行插入操作,id为表中的一个列名
db.insert("person","id",values);
//执行删除操作,在person表中删除id为指定值的记录
db.delete("person","id=?",0); padding:0px; margin:0px; width:auto; border:0px">//要更新的数据
//更新person表中id为指定值的记录
db.update("person",values,85); font-weight:bold">newString[]{String.valueOf(p.getId())});
//执行查询:不去重复,表是person,查询name和balance两列,Where条件是"id=?",占位符是id,不分组,没有having,不排序,没有分页
Cursorc=db.query(false,"person",85); font-weight:bold">newString[]{"name","balance"},85); font-weight:bold">newString[]{String.valueOf(id)},85); font-weight:bold">null,0); padding:0px; margin:0px; width:auto; border:0px">//查询所有记录,倒序
newString[]{"id","name","idDESC",0); padding:0px; margin:0px; width:auto; border:0px">//翻页查询
","+length);
newString[]{"COUNT(*)"},250); line-height:18px"> }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|