Layout file
Created four keys, respectively, the corresponding additions and deletions to change
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"android:orientation= "vertical"Tools:context= "Xidian.dy.com.chujia.MainActivity"> <ButtonAndroid:id= "@+id/btn_insert"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Insert a piece of data"/> <ButtonAndroid:id= "@+id/btn_query"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Query Current Data" /> <ButtonAndroid:id= "@+id/btn_update"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Modify a piece of data"/> <ButtonAndroid:id= "@+id/btn_delete"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Delete One piece of data"/></LinearLayout>
Database operations
PackageXidian.dy.com.chujia;Importandroid.content.ContentValues;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener {PrivateSqlitedatabase DB; Privatemainactivity Mcontext; Private inti = 1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Mcontext= Mainactivity. This; Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mydbopenhelper Mydbhelper=NewMydbopenhelper (Mcontext, "my.db",NULL, 1); DB=mydbhelper.getwritabledatabase (); Bindviews (); } Private voidbindviews () {Button Btn_insert=(Button) Findviewbyid (R.id.btn_insert); Button Btn_query=(Button) Findviewbyid (r.id.btn_query); Button btn_update=(Button) Findviewbyid (r.id.btn_update); Button Btn_delete=(Button) Findviewbyid (r.id.btn_delete); if(Btn_query! =NULL) Btn_query.setonclicklistener ( This); if(Btn_insert! =NULL) Btn_insert.setonclicklistener ( This); if(Btn_update! =NULL) Btn_update.setonclicklistener ( This); if(Btn_delete! =NULL) Btn_delete.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.btn_insert:contentvalues values1=Newcontentvalues (); //name is the name of the property in the database, and the second is the value you want to insertValues1.put ("name", "hehe ~" +i); I++; //The parameter is: Table name, forcibly inserting null column name of the data column, a row of recorded dataDb.insert ("Person",NULL, values1); Toast.maketext (Mcontext,"Insert Complete ~", Toast.length_short). Show (); Break; CaseR.id.btn_query:stringbuilder SB=NewStringBuilder (); //The parameters are: Table name, column name, where constraint, where placeholder provides a specific value, specify group By column, further constrain//Specify how query results are sortedcursor cursor = db.query ("Person",NULL,NULL,NULL,NULL,NULL,NULL); if(Cursor.movetofirst ()) { Do { intid = cursor.getint (cursor.getcolumnindex ("_id")); String name= Cursor.getstring (Cursor.getcolumnindex ("name")); Sb.append ("ID:" + ID + ":" + name + "\ n"); } while(Cursor.movetonext ()); } cursor.close (); Toast.maketext (Mcontext, sb.tostring (), Toast.length_short). Show (); Break; Caser.id.btn_update:contentvalues Values2=Newcontentvalues (); Values2.put ("Name", "hehe ~"); //The parameter is the table name, the modified value, the Where condition, and the constraint, and if you do not specify 342 parameters, all rows are changedDb.update ("Person", values2, "name =?",Newstring[]{"}); Break; CaseR.id.btn_delete://parameters are table names, where conditions, constraintsDb.delete ("Person", "_id =?",Newstring[]{"3"}); //Multiple conditions//db.delete ("person", "_id=?") And name=? ", New string[]{" 1 "," nickname "}); Break; } }}
This first creates a database helper that gets the database operand from the helper. Use the APIs provided by this object to manipulate the database.
Database helper Classes
PackageXidian.dy.com.chujia;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper;/*** Created by Dy on 2016/6/13.*/ Public classMydbopenhelperextendsSqliteopenhelper { PublicMydbopenhelper (Context context, String name, Sqlitedatabase.cursorfactory factory,intVersion) {Super(Context, "my.db",NULL, 1); } @Override//called when the database was first created Public voidonCreate (Sqlitedatabase db) {Db.execsql ("CREATE TABLE person (_id INTEGER PRIMARY KEY autoincrement,name VARCHAR (20))"); } //called when the software version number has changed@Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {Db.execsql ("ALTER TABLE person ADD phone VARCHAR () NULL"); }}
Transaction
If you use a transaction in a database operation, you can refer to the following code
//Transfer Things Public voidtransaction () {Try{db.begintransaction (); Contentvalues Values=Newcontentvalues (); Values.put ("Salary", 9000); Db.update ("Person", values, "Name=?",Newstring[]{"Xiao Wang"}); Values.clear (); Values.put ("Salary", 11000); Db.update ("Person", values, "Name=?",Newstring[]{"Xiao Zhang"}); //SET Transaction Execution Successdb.settransactionsuccessful (); }finally { //close things, commit at the same time, if the transaction execution has been set successfully, then SQL will take effect, otherwise SQL rollbackdb.endtransaction (); } }
Android's sqllite operation