Sqliteopenhelper Introduction:
Sqliteopenhelper is an excuse! So you can't instantiate it directly! We need to implement this interface if we want to get the Sqliteopenhelper Object! Create an implementation class object for this interface!
This object has the following common methods:
getreadabledatabase () opens or creates a database and returns a Sqlitedatabase object;
getwritabledatabase () opens or creates a database and returns a Sqlitedatabase object;
The difference between getreadabledatabase () and getwritabledatabase () it:
When you use getreadabledatabase () to get the Sqlitedatabase object if the database is not writable (such as disk space is full) thegetreadabledatabase() method returns the You can only read data from this database when you sqlitedatabase an Object
The method getreadabledatabase() will cause an exception
abstract void
|
OnCreate (Sqlitedatabase db) |
This method is only called when you create the database for the first time! If the database is already in the program, the method will not be called again! The usual table initialization is written in this method!
abstract void
|
Onupgrade (sqlitedatabase db, int oldversion, int newversion) |
This method is called when the database version is updated! We will set up a database version when we create the database! This method is called if there is a change to the version of the database!
synchronized void
This method closes the database!
String |
GetDatabaseName () |
Get the database name!
Customize our own Sqliteopenhelper
1 Public classMysqlitehelperextendsSqliteopenhelper {2 Private StaticString My_sql;3 Privatecontext context;4 PublicMysqlitehelper (Context context, String name, Cursorfactory factory,5 intversion) {6 Super(Context, Name,NULL, version);7 This. Context =context;8 }9 Ten @Override One Public voidonCreate (Sqlitedatabase db) { AMy_sql = "CREATE TABLE MYTABLE (_id INTEGER PRIMARY key,_name,_age,_address)"; - Db.execsql (my_sql); - } the - @Override - Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { -Toast.maketext (Context, "old version:" +oldversion+ ", new version" +newversion, 0). Show (); + } - +}
To manipulate the database using Mysqlitehelper:
1 Public classMainactivityextendsActivityImplementsonclicklistener{2 PrivateButton Btnquery,btnupdate,btnadd,btndelete;3 PrivateMysqlitehelper Mysqlitehelper;4 Private intVersion = 2;5 Privatesqlitedatabase database;6 Privatecontentvalues contentvalues;7 @Override8 protected voidonCreate (Bundle savedinstancestate) {9 Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_main); One Initview (); A setEvent (); - } - the Private voidsetEvent () { -Btnquery.setonclicklistener ( This); -Btnupdate.setonclicklistener ( This); -Btnadd.setonclicklistener ( This); +Btndelete.setonclicklistener ( This); - } + A Private voidInitview () { atBtnadd = (Button) This. Findviewbyid (r.id.id_databasebuttonadd); -Btndelete = (Button) This. Findviewbyid (r.id.id_databasebuttondelete); -Btnquery = (Button) This. Findviewbyid (r.id.id_databasebuttonquery); -Btnupdate = (Button) This. Findviewbyid (r.id.id_databasebuttonupdate); -Mysqlitehelper =NewMysqlitehelper ( This, "Mysqlite.db",NULL, version);//Create a database -Database = Mysqlitehelper.getreadabledatabase ();//Open Database return Sqlitedatabase object inContentvalues =Newcontentvalues (); - } to + @Override - Public voidOnClick (View v) { the Switch(V.getid ()) { * CaseR.id.id_databasebuttonadd: $ contentvalues.clear ();Panax NotoginsengContentvalues.put ("_name", "QQ"); -Contentvalues.put ("_age", "15"); theContentvalues.put ("_address", "NO"); +Database.insert ("MYTABLE",NULL, contentvalues); A Break; the CaseR.id.id_databasebuttonquery: +cursor cursor = database.query ("MYTABLE",NULL,NULL,NULL,NULL,NULL,NULL); - while(Cursor.movetonext ()) { $String name =cursor.getstring (Cursor.getcolumnindex ("_name")); $String ID =cursor.getstring (Cursor.getcolumnindex ("_id")); -String Age =cursor.getstring (Cursor.getcolumnindex ("_age")); -String Address =cursor.getstring (Cursor.getcolumnindex ("_address")); theToast.maketext ( This, id+ "|" +name+ "|" +age+ "|" +address, 0). Show (); - }Wuyi Break; the CaseR.id.id_databasebuttondelete: - //The same operation as in the previous article Wu Break; - Caser.id.id_databasebuttonupdate: About //The same operation as in the previous article $ Break; - - default: - Break; A } + } the -}
XML file:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Fill_parent"3 Android:layout_height= "Fill_parent"4 android:orientation= "vertical" >5 6 7 <Button8 Android:id= "@+id/id_databasebuttonadd"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One Android:text= "Add Data" /> A - <Button - Android:id= "@+id/id_databasebuttonupdate" the Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:text= "Modify Data" /> - + <Button - Android:id= "@+id/id_databasebuttondelete" + Android:layout_width= "Match_parent" A Android:layout_height= "Wrap_content" at Android:text= "Delete Data" /> - - <Button - Android:id= "@+id/id_databasebuttonquery" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" in Android:text= "Query data" /> - </LinearLayout>
Simple use of the Sqlitedatabase Sqliteopenhelper class for Android data storage