歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android學習筆記之SQLite數據庫存儲

Android學習筆記之SQLite數據庫存儲

日期:2017/3/1 10:41:35   编辑:Linux編程

因為前面提到xml存儲更改文件很麻煩的緣故( 見http://www.linuxidc.com/Linux/2012-01/50684.htm ),最終還是選擇了使用數據庫存儲

一試才覺十分的方便,速度也快

上源碼:

  1. public class DBHelper extends SQLiteOpenHelper{
  2. private final static String DATABASE_NAME="fanliao_db";
  3. private final static int DATABASE_VERSION=1;
  4. private final static String TABLE_NAME="fanliao_chat";
  5. public final static String CHAT_ID="_id";
  6. public final static String CHAT_Name="chatname";
  7. public final static String CHAT_Info="chatinfo";
  8. public final static String CHAT_Time="chattime";
  9. public DBHelper(Context context)
  10. {
  11. super(context, DATABASE_NAME,null, DATABASE_VERSION);
  12. }
  13. @Override
  14. public void onCreate(SQLiteDatabase db) {
  15. //CREATE TABLE fanliao_chat( _id INTEGER PRIMARY KEY AUTOINCREMENT,
  16. // chatname TEXT, chattime TEXT, chatinfo TEXT);
  17. String sql="CREATE TABLE "+TABLE_NAME+"("+CHAT_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
  18. +CHAT_Name+" TEXT, "+CHAT_Time+" TEXT, "+CHAT_Info+" TEXT);";
  19. db.execSQL(sql);
  20. System.out.println(sql);
  21. }
  22. @Override
  23. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  24. String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;
  25. db.execSQL(sql);
  26. onCreate(db);
  27. System.out.println(sql);
  28. }
  29. public Cursor select()
  30. {
  31. SQLiteDatabase db=this.getReadableDatabase();
  32. Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null, " _id asc");
  33. return cursor;
  34. }
  35. public long insert(String chatname, String chattime, String chatinfo)
  36. {
  37. SQLiteDatabase db=this.getWritableDatabase();
  38. ContentValues cv=new ContentValues();
  39. cv.put(CHAT_Name, chatname);
  40. cv.put(CHAT_Time, chattime);
  41. cv.put(CHAT_Info, chatinfo);
  42. long row=db.insert(TABLE_NAME, null, cv);
  43. return row;
  44. }
  45. public void delete(int id)
  46. {
  47. SQLiteDatabase db=this.getWritableDatabase();
  48. String where=CHAT_ID+"=?";
  49. String[] whereValue={Integer.toString(id)};
  50. db.delete(TABLE_NAME, where, whereValue);
  51. }
  52. public void update(int id,String chatname,String chattime, String chatinfo)
  53. {
  54. SQLiteDatabase db=this.getWritableDatabase();
  55. String where=CHAT_ID+"=?";
  56. String[] whereValue={Integer.toString(id)};
  57. ContentValues cv=new ContentValues();
  58. cv.put(CHAT_Name, chatname);
  59. cv.put(CHAT_Time, chattime);
  60. cv.put(CHAT_Info, chatinfo);
  61. db.update(TABLE_NAME, cv, where, whereValue);
  62. }
  63. public void delall(){
  64. SQLiteDatabase db=this.getReadableDatabase();
  65. String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;
  66. db.execSQL(sql);
  67. onCreate(db);
  68. }
  69. }
用後才覺得經常修改的數據本就應用數據庫的,

形如“聊天記錄”這種雖沒有十分復雜的存儲結構,也是適宜存在表中,

而xml大概多是用以傳輸數據或存儲少量不常用改動的數據把~

Copyright © Linux教程網 All Rights Reserved