In the development of Android will inevitably encounter in the phone to save data, if only a small amount of data (such as saving settings, etc.), with Sharedpreferences is an excellent choice, it is a key-value pair in the form of saving data, but if the volume of data is more, For example, a key corresponding to a set of circumstances, at this time again with Sharedpreferences to save the data is difficult, if you need to modify the data to delete the operation, the method of saving data is obviously not suitable, so the android itself has built-in SQLite database, It's enough to save the data in the app.
New Android project to create a new class: SQLDatabase. Java, which inherits from Sqliteopenhelper, is used primarily to create new databases, create new data tables, and update databases:
1 Public classSQLDatabaseextendssqliteopenhelper{2 Privatecontext context;3 PrivateString creat_book= "CREATE TABLE book (ID integer primary key autoincrement,author text,price real,pages integer,name text) ";4 PublicSQLDatabase (context context, String name, Sqlitedatabase.cursorfactory factory,intversion) {5 Super(context, name, Factory, version);6 This. Context =context;7 }8 @Override9 Public voidonCreate (Sqlitedatabase db) {Ten //Build Table One Db.execsql (creat_book); A //Insert some data -Toast.maketext (Context, "CREATE database succeeded", Toast.length_short). Show (); - } the @Override - Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { -Toast.maketext (Context, "database updated", Toast.length_short). Show (); - } +}
Layout file:
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Xmlns:tools= "Http://schemas.android.com/tools"4Android:id= "@+id/activity_main"5Android:layout_width= "Match_parent"6android:layout_height= "Match_parent"7android:orientation= "Vertical"8tools:context= "Com.example.mydiyizho3_2.MainActivity" >9<ButtonTenAndroid:id= "@+id/button1" OneAndroid:layout_width= "Match_parent" Aandroid:layout_height= "Wrap_content" -android:onclick= "OnClick" -android:text= "Increase"/> the<Button -Android:id= "@+id/button2" -Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" +android:onclick= "OnClick" -android:text= "Delete"/> +<Button AAndroid:id= "@+id/button3" atAndroid:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -android:onclick= "OnClick" -android:text= "Change"/> -<Button -Android:id= "@+id/button4" inAndroid:layout_width= "Match_parent" -android:layout_height= "Wrap_content" toandroid:onclick= "OnClick" +android:text= "Check"/> - the</LinearLayout>
The next step is to add, delete, modify, and find the data:
1 Public classMainactivityextendsappcompatactivity {2 3 Private Static FinalString TAG = "Mainactivity";4 Privatesqldatabase database;5 Privatesqlitedatabase writabledatabase;6 7 @Override8 protected voidonCreate (Bundle savedinstancestate) {9 Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_main); OneDatabase =NewSQLDatabase (mainactivity. This, "Bookstore.db",NULL, 1); A } - - Public voidOnClick (view view) { the Switch(View.getid ()) { - CaseR.id.button1://Increase -Writabledatabase =database.getwritabledatabase (); -Contentvalues values =Newcontentvalues (); + for(inti = 0; I <20; i++) { -Values.put ("Author", "Homeng" +i); +Values.put ("Price", 10.5+i); AValues.put ("pages", 1000+i); atValues.put ("name", "acrobatic troupe" +i); - LongBook = Writabledatabase.insert ("book",NULL, values); -LOG.I (TAG, "id= per day" +Book ); - } - //Close - writabledatabase.close (); in Break; - CaseR.id.button2://Delete toWritabledatabase =database.getwritabledatabase (); + intBook = Writabledatabase.delete ("book", "Id=?",NewString[]{1 + ""}); -LOG.I (TAG, "This is deleted" +Book ); the writabledatabase.close (); * Break; $ CaseR.id.button3://ChangePanax NotoginsengWritabledatabase =database.getwritabledatabase (); -Contentvalues update =Newcontentvalues (); theUpdate.put ("Price", 10000); + intUpdate1 = Writabledatabase.update ("book", Update, "Author=?",Newstring[]{"Homeng5"}); ALOG.I (TAG, "This is modified" +update1); the writabledatabase.close (); + Break; - CaseR.id.button4://Check $Writabledatabase =database.getwritabledatabase (); $Cursor Book1 = Writabledatabase.query ("book",NULL,NULL,NULL,NULL,NULL,NULL); - if(Book1.movetofirst ()) { - Do { theString name = book1.getstring (Book1.getcolumnindex ("name")); - floatPrice = Book1.getfloat (Book1.getcolumnindex ("Price"));Wuyi intPages = Book1.getint (Book1.getcolumnindex ("pages")); theString author = book1.getstring (Book1.getcolumnindex ("Author")); -LOG.I (TAG, "name" +name); WuLOG.I (TAG, "price" +Price ); -LOG.I (TAG, "pages" +pages); AboutLOG.I (TAG, "author" +author); $} while(Book1.movetonext ()); - } - writabledatabase.close (); - Break; A } + } the}
SQLite database additions and deletions to search