[Android Study Notes] SQLite database storage

Source: Internet
Author: User

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 ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.