Finally, write the activity by clicking on the event to manipulate these methods
Packagecom.example.sqlite1;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener; Public classMainactivityextendsActivity {PrivateSqliteopenhelper Hel; PrivateMytab Mytab; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Hel=NewHelper ( This); Findviewbyid (R.ID.B1). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Mytab=NewMytab (Hel.getwritabledatabase ()); Mytab.insert ("Ming", "1992-01-16"); } }); Findviewbyid (R.ID.B2). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Mytab=NewMytab (Hel.getwritabledatabase ()); Mytab.update (101, "haha", "2015-10-02"); } }); Findviewbyid (r.id.b3). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Mytab=NewMytab (Hel.getwritabledatabase ()); Mytab.delete (102); } }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
We're going to create a table first let a class inherit Sqliteopenhelper and then write the table statement in the OnCreate method
Packagecom.example.sqlite1;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper; Public classHelperextendsSqliteopenhelper {Private Static FinalString DBNAME = "Neusoft.db"; Private Static Final intDbversion = 1; Private Static FinalString TABLENAME = "Mytab"; PublicHelper (Context context) {Super(Context, DBNAME,NULL, dbversion); //TODO auto-generated Constructor stub} @Override Public voidonCreate (Sqlitedatabase db) {//TODO auto-generated Method StubString sql = "CREATE TABLE" +tablename+ "(" + "ID integer primary key," + "name varchar () n OT null, "+" Birthday date not NULL "+") "; Db.execsql (SQL); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { //TODO auto-generated Method Stub }}
And then write a specific operation of the class
Packagecom.example.sqlite1;ImportAndroid.database.sqlite.SQLiteDatabase;PublicClassMytab {Private String TableName = "Mytab";PrivateSqlitedatabase DB;PublicMytab (Sqlitedatabase db) {This.db =db }Publicvoid insert (string name, string birthday) {String sql = " INSERT INTO "+ TableName +" (name,birthday) VALUES (' "+ name +" ', ' "+ Birthday +" ') "public void Update ( Int ID, string name, string birthday) {String sql = "Update" + TableName + "set name= '" + Name + "', birthday= '" + Birthday + "' where id= ' + ID; db.execsql (SQL); Db.close ();} public void Delete (int ID) {String sql = "Delete from" + TableName + "where id=" + ID; Db.execsql (SQL); Db.close (); }}
Packagecom.example.sqlite1;Importandroid.database.sqlite.SQLiteDatabase; Public classMytab {PrivateString tableName = "Mytab"; PrivateSqlitedatabase DB; PublicMytab (Sqlitedatabase db) { This. db =DB; } Public voidInsert (string name, string birthday) {//String sql = "INSERT INTO" + TableName + "(name,birthday) VALUES ('"//+ name + "', '" + Birthday + "')";String sql = "INSERT INTO" + TableName + "(name,birthday) VALUES (?,?)"; String[] s={name,birthday}; Db.execsql (sql,s); Db.close (); } Public voidUpdateintID, string name, string birthday) {//String sql = "Update" + TableName + "set name= '" + name + "', birthday= '"//+ birthday + "' where id=" + ID;String sql = "Update" + TableName + "set name=?,birthday=?,id=?"; Object[] o={Name,birthday,id}; Db.execsql (Sql,o); Db.close (); } Public voidDeleteintID) {String SQL= "Delete from" + TableName + "where id=" +ID; Db.execsql (SQL); Db.close (); }}
The use of contentvalues plus their own additions and deletions to modify the method
PackageCom.example.sqlite2;Importandroid.content.ContentValues;Importandroid.database.sqlite.SQLiteDatabase; Public classMytab {PrivateString tableName = "Mytab"; PrivateSqlitedatabase DB; PublicMytab (Sqlitedatabase db) { This. db =DB; } Public voidInsert (string name, string birthday) {contentvalues CV=Newcontentvalues (); Cv.put ("Name", name); Cv.put ("Birthday", birthday); Db.insert (TableName,NULL, CV); Db.close (); } Public voidUpdateintID, string name, string birthday) {contentvalues CV=Newcontentvalues (); Cv.put ("Name", name); Cv.put ("Birthday", birthday); String where= "Id=?"; String [] s={string.valueof (id)}; Db.update (TableName, CV, where, s); Db.close (); } Public voidDeleteintID) {contentvalues CV=Newcontentvalues (); String where= "Id=?"; String [] s={string.valueof (id)}; Db.delete (TableName, where, s); Db.close (); }}
The first thing to do is to match the path of the D:\adt-bundle-windows-x86-20131030\sdk\platform-tools to PATH environment variables.
Then go in and cmd type the ADB shell into the simulator.
Then access the data/data/package name/databases
Type the SQLITE3 database name again. db
You can manipulate the data.
The. Schema is the view table
Accessing the emulator database