Because it is difficult to change files in XML storage, we finally chose to use database storage.
A try is very convenient and fast
Source code:
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 d Bhelper (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.exe csql (SQL); system. out. println (SQL) ;}@ override public void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {string SQL = "Drop table if exists" + table_name; db.exe csql (SQL ); oncreate (db); system. out. println (SQL);} public cursor select () {sqlitedatabase DB = This. getreadabledatabase (); cursor = dB. query (table_name, 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.exe csql (SQL); oncreate (db );}}
After use, I feel that the data that is frequently modified is in the application database,
Although there is no complex storage structure such as "chat record", it is also suitable for tables,
XML is usually used to transmit data or store a small amount of data that is not frequently changed ~