Android as an operating system on mobile devices, it is natural that data storage. However, as a lightweight relational database, SQLite is based on many factors such as light weight, cross-platform, multi-lingual interface and security, so the larger data storage of Android uses SQLite. SQLite, like most relational databases, follows acid, and the syntax is very similar. As long as you understand the MySQL, SQL Server and other relational database operations, as long as you view the official files of SQLite can quickly get started. SQLite syntax You can view through the http://sqlite.org/lang.html.
Now let's see how Android operates with SQLite. With Android, you can easily manage sqlite by extending the Sqliteopenhelper interface and implementing its create and update methods. The following is a look at the database management class.
PackageCom.example.ibm.myappsqllite;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper;/*** Created by IBM on 2015/9/11.*/ Public classDbextendsSqliteopenhelper { PublicDb (Context context,intversion) { Super(Context, "DB",NULL, version);//automatic creation and upgrade of tables can be done based on version of the last parameter} @Override Public voidonCreate (Sqlitedatabase db) {String SQL= "CREATE TABLE User (" + "ID integer primary key autoincrement," + "name TEXT DEFAULT \" \ ", "+" Sex TEXT DEFAULT \ "\") "; System.out.println ("CREATE DATABASE:" +SQL); Db.execsql (SQL); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intNewVersion) {//Update DatabaseString sql = "ALTER TABLE user ADD COLUMN mark TEXT DEFAULT \" \ ""; System.out.println ("Update Database:" +SQL); Db.execsql (SQL); }}
Add the following code to the OnCreate method in Mainactivity.java.
DB db =NewDb ( This, 1); //Clear DataSqlitedatabase Sqlitedb =db.getwritabledatabase (); System.out.println ("------------clear the table data------------"); Sqlitedb.delete ("User",NULL,NULL); System.out.println ("------------recharge table self-increment ID------------"); Sqlitedb.execsql ("Update sqlite_sequence SET seq = 0 WHERE name = ' user ';");//Reset IDsqlitedb.close ();//Write DataSystem.out.println ("------------Write Data------------"); Sqlitedb=db.getwritabledatabase (); Contentvalues CV=Newcontentvalues (); Cv.put ("Name", "Harry"); Cv.put ("Sex", "male"); Sqlitedb.insert ("User",NULL, CV); CV=Newcontentvalues (); Cv.put ("Name", "Lily"); Cv.put ("Sex", "female"); Sqlitedb.insert ("User",NULL, CV); Sqlitedb.close ();//Querying Datadb =NewDb ( This, 2); System.out.println ("------------query data 1------------"); Sqlitedatabase Sqlitedbup=db.getreadabledatabase (); Cursor C= Sqlitedbup.query ("User",Newstring[]{"id", "name", "Sex"},NULL,NULL,NULL,NULL,NULL); while(C.movetonext ()) {Integer ID= C.getint (C.getcolumnindex ("id")); String name= C.getstring (C.getcolumnindex ("name")); String Sex= C.getstring (C.getcolumnindex ("Sex"))); System.out.println (String.Format ("ID:%d, Name:%s, Sex:%s", ID, name, sex)); } sqlitedbup.close ();//Update DataSYSTEM.OUT.PRINTLN ("------------Update data------------"); Sqlitedatabase Sqldb=db.getwritabledatabase (); Contentvalues contentvalues=Newcontentvalues (); Contentvalues.put ("Sex", "gay"); Contentvalues.put ("Mark", "Wang Wu is gay"); String[] Wherevalues= {"Harry"}; Sqldb.update ("User", Contentvalues, "name =?", wherevalues); Sqldb.close ();//Querying DataSYSTEM.OUT.PRINTLN ("------------query data 2------------"); Sqlitedbup=db.getreadabledatabase (); C= Sqlitedbup.query ("User",Newstring[]{"id", "name", "Sex", "Mark"},NULL,NULL,NULL,NULL,NULL); while(C.movetonext ()) {Integer ID= C.getint (C.getcolumnindex ("id")); String name= C.getstring (C.getcolumnindex ("name")); String Sex= C.getstring (C.getcolumnindex ("Sex"))); String Mark= C.getstring (C.getcolumnindex ("Mark")); System.out.println (String.Format ("ID:%d, Name:%s, sex:%s, Mark:%s", ID, name, sex, Mark); } sqlitedbup.close (); //Delete DataSystem.out.println ("------------Delete data------------"); Sqldb=db.getwritabledatabase (); String[] Delwhere= {"Lily"}; Sqldb.delete ("User", "name =?", Delwhere); Sqldb.close ();//Querying DataSYSTEM.OUT.PRINTLN ("------------query data 3------------"); Sqlitedbup=db.getreadabledatabase (); C= Sqlitedbup.query ("User",Newstring[]{"id", "name", "Sex", "Mark"},NULL,NULL,NULL,NULL,NULL); while(C.movetonext ()) {Integer ID= C.getint (C.getcolumnindex ("id")); String name= C.getstring (C.getcolumnindex ("name")); String Sex= C.getstring (C.getcolumnindex ("Sex"))); String Mark= C.getstring (C.getcolumnindex ("Mark")); System.out.println (String.Format ("ID:%d, Name:%s, sex:%s, Mark:%s", ID, name, sex, Mark); } sqlitedbup.close (); Db.close ();
After you run the app to the virtual machine, you can see the following results
09-11 18:58:29.460 6647-6647/com.example.ibm.myappsqllite i/system.out: Creating a database: Create TABLE User (ID integer primary key AutoIncrement, Name text default "", Sex text Default "")09-11 18:58:29.500 6647-6647/com.example.ibm.myappsqllite i/system.out:------------emptying table data------------09-11 18:58:29.532 6647-6647/com.example.ibm.myappsqllite i/system.out:------------recharge Table self-increment ID------------09-11 18:58:29.532 6647-6647/com.example.ibm.myappsqllite i/system.out:------------Write Data------------09-11 18:58:29.616 66 47-6647/com.example.ibm.myappsqllite i/system.out:------------Query data 1------------09-11 18:58:29.628 6647-6647/ Com.example.ibm.myappsqllite i/system.out: Update Database: ALTER TABLE user ADD COLUMN mark TEXT DEFAULT "" 09-11 18:58:29.640 664 7-6647/com.example.ibm.myappsqllite i/system.out:id:1, Name: Harry, Sex: Male09-11 18:58:29.644 6647-6647/com.example.ibm.myappsqllite I/system.out:id:2, Name: Lily, Sex: female09-11 18:58:29.644 6647-6647/com.example.ibm.myappsqllite i/system.out:------------Update data------------09-11 18:58:29.668 6647-6647/com.example.ibm.myappsqllite i/system.out:------------query data 2------------09-11 18:58:29.684 6 647-6647/com.example.ibm.myappsqllite i/system.out:id:1, Name: Harry, Sex:gay, Mark:wang Wu is gay09-11 18:58:29.684 6647-6647/com.example.ibm.myappsqllite I/system.out:id:2, Name: Lily, Sex: Female, Mark:09-11 18:58:29.688 6647-6647/com.example.ibm.myappsqllite i/system.out:------------Delete data------------09-11 18:58:29.708 6647-6647/com.example.ibm.myappsqllite i/system.out:------------query data 3------------09-11 18:58:29.716 6 647-6647/com.example.ibm.myappsqllite i/system.out:id:1, Name: Harry, Sex:gay, Mark:wang Wu is gay
At this point, you can view the/data/data/project package/databases/directory under the emulator, there is already a DB database (named previously defined database name).
Operation of SQLite for Android data store