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

SQLite数据库存储

发布时间:2020-12-12 23:44:50 所属栏目:百科 来源:网络整理
导读:因为前面提到xml存储更改文件很麻烦的缘故,最终还是选择了使用数据库存储 一试才觉十分的方便,速度也快 上源码: [java] view plain copy public class DBHelper extends SQLiteOpenHelper{ private final static StringDATABASE_NAME= "fanliao_db" ; sta

因为前面提到xml存储更改文件很麻烦的缘故,最终还是选择了使用数据库存储

一试才觉十分的方便,速度也快

上源码:

[java] view plain copy
  1. publicclassDBHelperextendsSQLiteOpenHelper{
  2. privatefinalstaticStringDATABASE_NAME="fanliao_db";
  3. staticintDATABASE_VERSION=1;
  4. staticStringTABLE_NAME="fanliao_chat";
  5. staticStringCHAT_ID="_id";
  6. staticStringCHAT_Name="chatname";
  7. staticStringCHAT_Info="chatinfo";
  8. staticStringCHAT_Time="chattime";
  9. publicDBHelper(Contextcontext)
  10. {
  11. super(context,DATABASE_NAME,null,DATABASE_VERSION);
  12. }
  13. @Override
  14. voidonCreate(SQLiteDatabasedb){
  15. //CREATETABLEfanliao_chat(_idINTEGERPRIMARYKEYAUTOINCREMENT,
  16. //chatnameTEXT,chattimeTEXT,chatinfoTEXT);
  17. Stringsql="CREATETABLE"+TABLE_NAME+"("+CHAT_ID+"INTEGERPRIMARYKEYAUTOINCREMENT,"
  18. +CHAT_Name+"TEXT,"+CHAT_Time+"TEXT,"+CHAT_Info+"TEXT);";
  19. db.execSQL(sql);
  20. System.out.println(sql);
  21. }
  22. @Override
  23. voidonUpgrade(SQLiteDatabasedb,153); font-weight:bold; background-color:inherit">intoldVersion,153); font-weight:bold; background-color:inherit">intnewVersion){
  24. Stringsql="DROPTABLEIFEXISTS"+TABLE_NAME;
  25. onCreate(db);
  26. System.out.println(sql);
  27. publicCursorselect()
  28. SQLiteDatabasedb=this.getReadableDatabase();
  29. Cursorcursor=db.query(TABLE_NAME,"_idasc");
  30. returncursor;
  31. longinsert(Stringchatname,Stringchattime,Stringchatinfo)
  32. {
  33. SQLiteDatabasedb=this.getWritableDatabase();
  34. ContentValuescv=newContentValues();
  35. cv.put(CHAT_Name,chatname);
  36. cv.put(CHAT_Time,chattime);
  37. cv.put(CHAT_Info,chatinfo);
  38. longrow=db.insert(TABLE_NAME,cv);
  39. returnrow;
  40. voiddelete(intid)
  41. this.getWritableDatabase();
  42. Stringwhere=CHAT_ID+"=?";
  43. String[]whereValue={Integer.toString(id)};
  44. db.delete(TABLE_NAME,where,whereValue);
  45. voidupdate(intid,Stringchatname,Stringchatinfo)
  46. ContentValuescv=newContentValues();
  47. cv.put(CHAT_Name,chatname);
  48. cv.put(CHAT_Time,chattime);
  49. cv.put(CHAT_Info,chatinfo);
  50. db.update(TABLE_NAME,cv,153); font-weight:bold; background-color:inherit">voiddelall(){
  51. this.getReadableDatabase();
  52. }

用后才觉得经常修改的数据本就应用数据库的,

形如“聊天记录”这种虽没有十分复杂的存储结构,也是适宜存在表中,

而xml大概多是用以传输数据或存储少量不常用改动的数据把~

(编辑:李大同)

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

    推荐文章
      热点阅读