加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite 基本操作

发布时间:2020-12-12 19:52:50 所属栏目:百科 来源:网络整理
导读:Android SQLite数据库操作实例 博客分类: android Android的自带数据库SQLite小巧且功能强大,Android提供了两种方式去操作数据库,第一种是用SQL语句去操作数据,SQLite支持标准的SQL,其分页等操作与Mysql一样,以下是利用SQL操作SQLite: Java代码 impor

Android SQLite数据库操作实例

    博客分类:
  • android

Android的自带数据库SQLite小巧且功能强大,Android提供了两种方式去操作数据库,第一种是用SQL语句去操作数据,SQLite支持标准的SQL,其分页等操作与Mysql一样,以下是利用SQL操作SQLite:

Java代码
  1. importjava.util.ArrayList;
  2. importjava.util.List;
  3. importandroid.content.Context;
  4. importandroid.database.Cursor;
  5. importandroid.database.sqlite.SQLiteDatabase;
  6. publicclassPersonDaoClassic{
  7. privateDBOpenHelperhelper;
  8. publicPersonDaoClassic(Contextcontext){
  9. helper=newDBOpenHelper(context);
  10. }
  11. voidinsert(Personp){
  12. //打开可写数据库
  13. SQLiteDatabasedb=helper.getWritableDatabase();
  14. //执行SQL语句,替换占位符
  15. db.execSQL("INSERTINTOperson(name,balance)VALUES(?,?)",newObject[]{p.getName(),p.getBalance()});
  16. //释放资源
  17. db.close();
  18. voiddelete(intid){
  19. db.execSQL("DELETEFROMpersonWHEREid=?",85); font-weight:bold">newObject[]{id});
  20. voidupdate(Personp){
  21. db.execSQL("UPDATEpersonSETname=?,balance=?WHEREid=?",p.getBalance(),p.getId()});
  22. publicPersonquery( SQLiteDatabasedb=helper.getReadableDatabase();
  23. //执行原始查询,得到一个Cursor(类似ResultSet)
  24. Cursorc=db.rawQuery("SELECTname,balanceFROMpersonWHEREid=?",85); font-weight:bold">newString[]{String.valueOf(id)});
  25. Personp=null;
  26. //判断Cursor是否有下一条记录
  27. if(c.moveToNext())
  28. //从Cursor中获取数据,创建Person对象
  29. p=newPerson(id,c.getString(0),c.getInt(1));
  30. c.close();
  31. returnp;
  32. publicList<Person>queryAll(){
  33. Cursorc=db.rawQuery("SELECTid,name,balanceFROMperson",85); font-weight:bold">null);
  34. List<Person>persons=newArrayList<Person>();
  35. while(c.moveToNext())
  36. persons.add(newPerson(c.getInt(1),0)">2)));
  37. returnpersons;
  38. publicList<Person>queryPage(intpageNum,85); font-weight:bold">intcapacity){
  39. //开始索引
  40. Stringstart=String.valueOf((pageNum-1)*capacity);
  41. //查询的个数
  42. Stringlength=String.valueOf(capacity);
  43. //翻页查询语句,和MySQL中相同
  44. ,85); font-weight:bold">newString[]{start,length});
  45. intqueryCount(){
  46. //查询记录条数
  47. Cursorc=db.rawQuery("SELECTCOUNT(*)FROMperson",250); line-height:18px"> c.moveToNext();
  48. intcount=c.getInt(0);
  49. returncount;
  50. }

除上述方法以外,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"> }
  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读