Use of Android SQLite

Source: Internet
Author: User
Tags sqlite

Take a rough look at how the Android database is handled. There are 4 types of data stored locally in Android: Sharepreference, SQLite, Content provider, and file, all stored in the data/data/package name directory. As you can see, Android is largely privatized for data.

SQLite is a lightweight database, currently mobile device side, the basic use of SQLite. For more information on the use of SQLite, see here: http://www.sqlite.org/

When using SQLite on Android, you typically operate two classes:sqlitedatabase and sqliteopenhelper

Sqliteopenhelper class

Class Description: A helper class to Manage database creation and version management. As you can see, this class is primarily used to create databases and to manage database version updates. Practical Applications:

 PackageCom.lanyuweng.mibaby.DataUtil;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportCom.lanyuweng.Config.Config; Public classDatabasehelperextendssqliteopenhelper{ PublicDatabasehelper (Context context) {Super(Context,config.dbname,NULL, config.version); }    //IF not EXISTS     Public Static FinalString sql_createtable = "CREATE TABLE noteitems (note_title varchar, note_content varchar, note_create_time datetime)";  Public Static FinalString sql_deletetable = "DROP TABLE IF EXISTS noteitems"; @Override Public voidonCreate (Sqlitedatabase db) {db.execsql (sql_createtable); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {                if(NewVersion >oldversion)        {Db.execsql (sql_deletetable); }Else{            return;            } onCreate (db); }} 

Replicate its OnCreate method, perform database creation in its method, and manage database updates in the Onupgrade method. Basic can be satisfied with the basic functions, more ways to find out more about the document a little more.

So the actual database operation, put it into a database management file, in this, can be increased, delete, change, check, or more database operation methods. The core is to use the Execsql (database statement) and look at the following:

 PackageCom.lanyuweng.mibaby.DataUtil;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase; Public classDatabasemanager { Publicsqlitedatabase sql_db;  PublicDatabasehelper Sql_db_helper; Privatecursor cursor;  PublicDatabasemanager (Context context) {Sql_db_helper=NewDatabasehelper (context); sql_db=sql_db_helper.getwritabledatabase (); }         Public voidAdd_noteitem (String note_title,string note_content,string note_create_time) {String sql= "INSERT into Noteitems (Note_title, note_content, Note_create_time) values" + "('" +note _title+ "', '" +note_content+ "', '" +note_create_time+ "')";                Sql_db.begintransaction (); Try{sql_db.execsql (SQL);        Sql_db.settransactionsuccessful (); }        finally{sql_db.endtransaction (); }    }         Public voidDel_noteitem (String note_title) {String sql= "DELETE from noteitems WHERE note_title= '" "+note_title+" ";        Sql_db.begintransaction (); Try{sql_db.execsql (SQL);        Sql_db.settransactionsuccessful (); }        finally{sql_db.endtransaction (); }    }         Public voidModify_noteiem (String old_note_title,string new_note_title,string note_content,string note_create_time) { String SQL= "UPDATE TABLE noteitems SET note_title=" +new_note_title+ ", note_content=" +note_content+ ", note_create_time=" +note_ Create_time+ "where note_title=" +Old_note_title;        Sql_db.begintransaction (); Try{sql_db.execsql (SQL);        Sql_db.settransactionsuccessful (); }        finally{sql_db.endtransaction (); }    }         PublicCursor Search_noteitem (String note_title) {String sql= "SELECT * from Noteitems WHERE note_title like" + "%" +note_title+ "%";        Sql_db.begintransaction (); Try{cursor= Sql_db.rawquery (SQL,NULL);        Sql_db.settransactionsuccessful (); }        finally{sql_db.endtransaction (); }        returncursor; }         PublicCursor Selectall_noteitems () {String SQL= "SELECT * FROM Noteitems";                Sql_db.begintransaction (); Try{cursor= Sql_db.rawquery (SQL,NULL); } finally{sql_db.endtransaction (); }        returncursor; }         PublicCursor Getlimititems (intStartintend) {String SQL= "SELECT * from Noteitems LIMIT" +start+ "," +end;        Sql_db.begintransaction (); Try{cursor= Sql_db.rawquery (SQL,NULL);                    Cursor.movetofirst (); } finally{sql_db.endtransaction (); }        returncursor; }    } 

Above, the implementation of the database additions and deletions of the logic, which has to mention a cursor class, cursor class can be convenient to manipulate the database. More about cursor use, recommend an article, use of the cursor

At this point, the operation of the Android database, basically the basic thing is these, if there are other, think of the re-fill.

A little more records, often looking back, will progress.

Use of Android SQLite

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.