Android学习笔记之SQLite数据库存储


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

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

上源码:

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

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

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

相关内容