The function of the project is to implement operations on the database, i.e. creat, UPDATE, INSERT, query, delete
When debugging, please use the simulator, when debugging with the real machine into the cmd-adb shell, and then into the CD Data/data will show permission denied
The following code is the code in Mainactivity.java
PackageCom.example.sqlite;ImportCom.example.sqlite.db.DataBaseHelper;Importandroid.app.Activity;Importandroid.content.ContentValues;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateButton Createdbbutton; PrivateButton Insertdbbutton; PrivateButton Updatedbbutton; PrivateButton Querydbbutton; PrivateButton Deletedbbutton; PrivateEditText input_id; PrivateEditText Input_name; PrivateTextView test; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //The database is created here and is implemented by a button listener.Createdbbutton =(Button) Findviewbyid (R.ID.CREATDB); Createdbbutton.setonclicklistener (NewCreatedblistener ()); //Here is the insert data into the databaseInsertdbbutton =(Button) Findviewbyid (R.id.insert); Insertdbbutton.setonclicklistener (NewInsertlistener ()); //Update data Table hereUpdatedbbutton =(Button) Findviewbyid (r.id.update); Updatedbbutton.setonclicklistener (NewUpdatelistener ()); //Update data Table hereQuerydbbutton =(Button) Findviewbyid (r.id.query); Querydbbutton.setonclicklistener (NewQuerylistener ()); //to delete data content hereDeletedbbutton =(Button) Findviewbyid (r.id.delete); Deletedbbutton.setonclicklistener (NewDeletelistener ()); //here to display query resultsTest =(TextView) Findviewbyid (R.id.result); Test.settext ("Name") ; //here is the option to add data//input_id = (EditText) Findviewbyid (r.id.input_id); //input_name = (EditText) Findviewbyid (r.id.input_name); } classCreatedblistenerImplementsonclicklistener{@Override Public voidOnClick (View v) {//here, you create a database by calling a method in another class, or you can create a database directlyString db_name= "Test_mars_db_one" ; System.out.println ("Create"); Databasehelper DBHelper=NewDatabasehelper (mainactivity. This, db_name); Sqlitedatabase DB=dbhelper.getreadabledatabase (); } } classInsertlistenerImplementsonclicklistener{@Override Public voidOnClick (View v) {//generate a Contentvalues objectContentvalues values =Newcontentvalues (); //You want to insert a key-value pair in the object, where the key is the column name, and the value is the value that you want to insert into the column, and the value mustValues.put ("id", 1) ; Values.put ("Name", "Zhangsan") ; System.out.println ("Insert"); Databasehelper DBHelper=NewDatabasehelper (mainactivity. This, "Test_mars_db_one") ; Sqlitedatabase DB=dbhelper.getwritabledatabase (); //you can insert data into the database by calling the Insert methodDb.insert ("User",NULL, values); } } classUpdatelistenerImplementsonclicklistener{@Override Public voidOnClick (View arg0) {//update the data content hereSystem.out.println ("Update"); Databasehelper DBHelper=NewDatabasehelper (mainactivity. This, "Test_mars_db_one") ; Sqlitedatabase DB=dbhelper.getwritabledatabase (); Contentvalues Values=Newcontentvalues (); Values.put ("Name", "Zhangsanfeng") ; //The first parameter is the name of the table to update//The second argument is a Contentvalues object//The third parameter is a where statementDb.update ("User", Values, "id=?",Newstring[]{"1"}) ; } } classQuerylistenerImplementsonclicklistener{@Override Public voidOnClick (View v) {//This is to query the data content and use it to the cursor to implementSYSTEM.OUT.PRINTLN ("Query"); Databasehelper DBHelper=NewDatabasehelper (mainactivity. This, "Test_mars_db_one") ; Sqlitedatabase DB=dbhelper.getwritabledatabase (); Cursor Cursor= Db.query ("User",Newstring[]{"id", "name"}, "Id=?",Newstring[]{"2"},NULL,NULL,NULL,NULL) ; while(Cursor.movetonext ()) {String name= Cursor.getstring (Cursor.getcolumnindex ("name")) ; System.out.print ("Query--->" +name); //name + = name;Test.settext (name); } } } classDeletelistenerImplementsonclicklistener{ Public voidOnClick (View v) {//Delete data for implementation hereSystem.out.println ("Delete"); Databasehelper DBHelper=NewDatabasehelper (mainactivity. This, "Test_mars_db_one") ; Sqlitedatabase DB=dbhelper.getwritabledatabase (); //Delete data for a specific condition//db.delete ("User", "id=?", New string[]{"2"}); //Delete all dataDb.delete ("User",NULL,NULL); } }}
The following code is the code in Databasehelper.java
Packagecom.example.sqlite.db;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper;//Databasehelper as an assistant class that accesses SQLite, offering two features//First, Getreadabledatabase (), getwriteabledatabase () can get the Sqlitedatabase object, which can manipulate the database//Second, OnCreate () and Onupgrade () Two callback functions are provided, allowing us to do our own work while creating and upgrading the database . Public classDatabasehelperextendsSqliteopenhelper {Private Static Final intVERSION = 1; //in Sqliteopenhelper subclasses, you must have this constructor PublicDatabasehelper (Context context, String name, Cursorfactory factory,intversion) { //constructors in the parent class must be called through Super Super(context, name, Factory, version); //TODO auto-generated Constructor stub } Publicdatabasehelper (Context context,string name) { This(context,name,version); } PublicDatabasehelper (Context context,string name,intversion) { This(Context,name,NULL, version); } //This function is executed when the database is first created and is actually called the first time the Sqlitedatabase object is obtained .@Override Public voidonCreate (Sqlitedatabase db) {//TODO auto-generated Method StubSystem.out.println ("Create a Database"); //The execsql function is used to execute SQL statementsDb.execsql ("CREATE table user (ID int,name varchar (20))"); System.out.println ("Create a Database successful"); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { //TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Update a DataBase"); }}
The following code is the code in Activity_main.xml
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/linearlayout1"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Vertical"Tools:context= "${relativepackage}.${activityclass}" > <Button Android:id= "@+id/creatdb"Android:text= "Creatdatabase"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> <Button Android:id= "@+id/update"Android:text= "Updatedatabase"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> <Button Android:id= "@+id/insert"Android:text= "Insert"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> <Button Android:id= "@+id/query"Android:text= "Query"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> <Button Android:id= "@+id/delete"Android:text= "Delete"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> <TextView Android:id= "@+id/result"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world"/></linearlayout>
Android Learning Note--sqlite