Android学习笔记之SQLite数据库存储
一试才觉十分的方便,速度也快
上源码:
- public class DBHelper extends SQLiteOpenHelper{
- private final static String DATABASE_NAME="fanliao_db";
- private final static int DATABASE_VERSION=1;
- private final static String TABLE_NAME="fanliao_chat";
- public final static String CHAT_ID="_id";
- public final static String CHAT_Name="chatname";
- public final static String CHAT_Info="chatinfo";
- public final static String CHAT_Time="chattime";
- public DBHelper(Context context)
- {
- super(context, DATABASE_NAME,null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- //CREATE TABLE fanliao_chat( _id INTEGER PRIMARY KEY AUTOINCREMENT,
- // chatname TEXT, chattime TEXT, chatinfo TEXT);
- String sql="CREATE TABLE "+TABLE_NAME+"("+CHAT_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
- +CHAT_Name+" TEXT, "+CHAT_Time+" TEXT, "+CHAT_Info+" TEXT);";
- db.execSQL(sql);
- System.out.println(sql);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;
- db.execSQL(sql);
- onCreate(db);
- System.out.println(sql);
- }
- public Cursor select()
- {
- SQLiteDatabase db=this.getReadableDatabase();
- Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null, " _id asc");
- return cursor;
- }
- public long insert(String chatname, String chattime, String chatinfo)
- {
- SQLiteDatabase db=this.getWritableDatabase();
- ContentValues cv=new ContentValues();
- cv.put(CHAT_Name, chatname);
- cv.put(CHAT_Time, chattime);
- cv.put(CHAT_Info, chatinfo);
- long row=db.insert(TABLE_NAME, null, cv);
- return row;
- }
- public void delete(int id)
- {
- SQLiteDatabase db=this.getWritableDatabase();
- String where=CHAT_ID+"=?";
- String[] whereValue={Integer.toString(id)};
- db.delete(TABLE_NAME, where, whereValue);
- }
- public void update(int id,String chatname,String chattime, String chatinfo)
- {
- SQLiteDatabase db=this.getWritableDatabase();
- String where=CHAT_ID+"=?";
- String[] whereValue={Integer.toString(id)};
- ContentValues cv=new ContentValues();
- cv.put(CHAT_Name, chatname);
- cv.put(CHAT_Time, chattime);
- cv.put(CHAT_Info, chatinfo);
- db.update(TABLE_NAME, cv, where, whereValue);
- }
- public void delall(){
- SQLiteDatabase db=this.getReadableDatabase();
- String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;
- db.execSQL(sql);
- onCreate(db);
- }
- }
形如“聊天记录”这种虽没有十分复杂的存储结构,也是适宜存在表中,
而xml大概多是用以传输数据或存储少量不常用改动的数据把~
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20