Related to the change of SQLite and delete, the results are shown by log
Package Com.example.sqlconnecttest;import Android.content.context;import android.database.sqlite.SQLiteDatabase; Import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.SQLiteOpenHelper; Import Android.util.log;public class DBHelper extends sqliteopenhelper{/* * Must have a constructor */public DBHelper (context context, St ring name, Cursorfactory Factory,int version) {Super (context, name, Factory, version);} /* * When the database is first created, the method is called */@Overridepublic void OnCreate (Sqlitedatabase db) {log.i ("createdatabases", "CREATE DATABASE--->");} /* * When updating the database, call the method */@Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {log.i ("UpdateData Base "," Update Database---> ");}}
Package Com.example.sqlconnecttest;import Android.app.activity;import Android.content.contentvalues;import Android.database.cursor;import Android.database.sqlexception;import android.database.sqlite.SQLiteDatabase; Import Android.os.bundle;import android.util.log;import Android.view.view;import Android.view.View.OnClickListener ; Import Android.widget.button;public class Mainactivity extends Activity implements Onclicklistener{private Button Createdatabase;private button createtable;private button insert;private button select;private button update;private Button Delete;private Final String database_name = "MyDatabase";p rivate sqlitedatabase mysqlitedatabase = null;private fi NAL String table_name = "User";p rivate string SQL = "CREATE TABLE [user] (" + "[id] INTEGER not NULL PRIMARY KEY AutoIncrement, "+" [username] varchar NOT NULL, "+" [password] varchar NOT NULL, "+" [ph Onenumber] VARCHAR not NULL) ";p rivate dbhelper db, @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); CreateView () ; Setlistener ();} private void CreateView () {createdatabase = (button) Findviewbyid (r.id.createdatabase); createtable = (Button) Findviewbyid (r.id.createtable), insert = (button) Findviewbyid (r.id.insert), select = (Button) Findviewbyid ( R.id.select); update = (Button) Findviewbyid (r.id.update);d elete = (button) Findviewbyid (R.id.delete);} Add Listener private void Setlistener () {Createdatabase.setonclicklistener (this), Createtable.setonclicklistener (this); Insert.setonclicklistener (This), Select.setonclicklistener (This), Update.setonclicklistener (this); Delete.setonclicklistener (this);} Various listening methods @overridepublic void OnClick (view view) {switch (View.getid ()) {Case r.id.createdatabase:{db = new DBHelper ( mainactivity.this,database_name,null,1); mysqlitedatabase = Db.getwritabledatabase (); LOG.I ("Database Object", Mysqlitedatabase.tostring ()),//system.out.print ("Database Object" +mysqlitedatabase); Case R.id.createtable:{db = new DBHelper (mainactivity.this, database_name, NULL, 1); mysqlitedatabase = Db.getwritabledatabase (); try { Mysqlitedatabase.execsql (SQL); LOG.I ("CREATE TABLE", SQL); catch (SQLException e) {e.printstacktrace (); LOG.I ("CREATE Table--", "CREATE TABLE failed----------------");} break;} Case r.id.insert:{db = new DBHelper (mainactivity.this, database_name, NULL, 1); mysqlitedatabase = Db.getwritabledatabase (); Contentvalues CV = new Contentvalues () cv.put ("username", "Dad"), Cv.put ("password", 123456); Cv.put ("PhoneNumber", " 134756658888 "); Long n = mysqlitedatabase.insert (table_name, NULL, CV); LOG.I ("Insert Data", n + ""); Mysqlitedatabase.close (); break;} Case r.id.select:{db = new DBHelper (mainactivity.this, database_name, NULL, 1); mysqlitedatabase = Db.getreadabledatabase ();/** * Parameter 1: Table name * Parameter 2: column to display * parameter 3:where statement * parameter 4:where statement condition value * Parameter 5: GROUP BY * parameter 6:having condition * Parameter 7: Sort by */c Ursor cursor = mysqlitedatabase.query (table_name, new string[]{"id", "username", "password", "PhoneNumber"}, NULL, NULL, NULL, NULL, NULL); while (Cursor.movetonExt ()) {int id = cursor.getint (cursor.getcolumnindex ("id")); String username = cursor.getstring (cursor.getcolumnindex ("username")); String password = cursor.getstring (cursor.getcolumnindex ("password")); String PhoneNumber = cursor.getstring (Cursor.getcolumnindex ("PhoneNumber")); LOG.I ("query-->", "ID:" + ID + "UserName:" + userName + "password" + password + "PhoneNumber" + PhoneNumber);} Mysqlitedatabase.close (); break;} Case r.id.update:{db = new DBHelper (mainactivity.this, database_name, NULL, 1); mysqlitedatabase = Db.getwritabledatabase (); Contentvalues cv1 = new Contentvalues () cv1.put ("username", "admin"), Cv1.put ("Password", "admin"); Cv1.put (" PhoneNumber "," 1388888 "); String whereclause = "id" + "=?"; String[] Whereargs = {"8"};/* * Parameter 1: Table name * Parameter 2: is a Contextvalue object, which is the updated value * parameter 3:where statement condition * parameter 4:where condition value */int index = mysqli Tedatabase.update (table_name, CV1, Whereclause, Whereargs); LOG.I ("update-->", Index + ""); Case r.id.delete:{db = new DBHelper (mainactivity.this, database_name, NULL, 1); mysqlitedatabase = Db.getwritabledatabase ();/*** * Parameter 1: Table name * parameter 2:where statement field * Parameter 3:where statement field value */string Whereclau SE1 = "id" + "=?"; String[] whereArgs1 = {"1"};int num = mysqlitedatabase.delete (table_name, WHERECLAUSE1, WHEREARGS1); LOG.I ("Delete record--", num + ""); Mysqlitedatabase.close (); break;} Default:break;}}}
<relativelayout xmlns: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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.example.sqlconnecttest.MainActivity" > <textview android:id= "@+id/textview" android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "SQLite database"/> < ; Button android:id= "@+id/createdatabase" android:layout_width= "fill_parent" android:layout_height= "WR Ap_content "android:layout_below=" @id/textview "android:text=" to create a database "/> <button an Droid:id= "@+id/createtable" android:layout_width= "fill_parent "android:layout_height=" wrap_content "android:layout_below=" @id/createdatabase "android:text= "Create a table"/> <button android:id= "@+id/insert" android:layout_width= "Fill_parent" Andro id:layout_height= "Wrap_content" android:layout_below= "@id/createtable" android:text= "Insert Data"/> & Lt Button android:id= "@+id/select" android:layout_width= "fill_parent" android:layout_height= "Wrap_conte NT "android:layout_below=" @id/insert "android:text=" query data "/> <button android:id=" @+i D/update "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:layout_belo w= "@id/select" android:text= "Update Data"/> <button android:id= "@+id/delete" android:layou T_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_below= "@id/update" Android: text= "Delete Data"/≫</relativelayout>
Run results
Basic operation of SQLite for Android