1. New Mysqlitehelper class inherits from Sqliteopenhelper
public class Mysqlitehelper extends Sqliteopenhelper {
Private context context;
Public Mysqlitehelper (Context context, String name, Cursorfactory factory,
int version) {
Super (context, name, Factory, version);
TODO auto-generated Constructor stub
This.context=context;
}
public static final String createcontact = "CREATE TABLE Contact ("
+ "ID integer primary key autoincrement,"
+ "name Text,phone text,email text)";
@Override
public void OnCreate (Sqlitedatabase arg0) {
TODO auto-generated Method Stub
Arg0.execsql (createcontact);
}
@Override
public void Onupgrade (sqlitedatabase arg0, int arg1, int arg2) {
TODO auto-generated Method Stub
}
}
2, the implementation of Add, modify, delete, query
public class Mainactivity extends Activity {
Mysqlitehelper Mysqlitehelper;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Calling the Getwritabledatabase () method, detects that there is no contact.db database in the current program, creates the database and invokes the OnCreate method in Mysqlitehelper to create the data table.
mysqlitehelper=new Mysqlitehelper (This, "contact.db", NULL, 1);
mysqlitehelper.getwritabledatabase ();
Button btninsert= (button) Findviewbyid (R.id.btninsert);
Btninsert.setonclicklistener (New Onclicklistener () {
Increase
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
sqlitedatabase db=mysqlitehelper.getwritabledatabase ();
contentvalues values=new contentvalues ();
values.put ("name", "AA");
values.put ("Phone", "13989999099");
Db.insert ("contact", null, values);
Values.clear ();
}
});
Button btnupdate= (button) Findviewbyid (r.id.btnupdate);
Btnupdate.setonclicklistener (New Onclicklistener () {
Modify
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
sqlitedatabase db=mysqlitehelper.getwritabledatabase ();
contentvalues values=new contentvalues ();
values.put ("email", "[email protected]");
db.update ("Contact", Values, "Name=", New string[]{"AA"});
Values.clear ();
}
});
Button btndelete= (button) Findviewbyid (R.id.btndelete);
Btndelete.setonclicklistener (New Onclicklistener () {
Delete
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
sqlitedatabase db=mysqlitehelper.getwritabledatabase ();
//db.delete ("Contact", "ID>?", New string[]{"1"});
Using SQL to manipulate databases directly
String sqlstring= "Delete from the contact where id= (select Max (ID) from the contact)";
Db.execsql (sqlString);
}
});
Button btnquery= (button) Findviewbyid (r.id.btnquery);
Btnquery.setonclicklistener (New Onclicklistener () {
Inquire
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Sqlitedatabase db=mysqlitehelper.getwritabledatabase ();
Using SQL queries directly
Cursor cursor=db.rawquery ("select * from contact where id>?", New string[]{"1"});
if (Cursor.movetofirst ()) {
Do {
int Id=cursor.getint (CURSOR.GETCOLUMNINDEX ("id"));
String namestring=cursor.getstring (Cursor.getcolumnindex ("name"));
String phonestring=cursor.getstring (Cursor.getcolumnindex ("Phone"));
String emailstring=cursor.getstring (cursor.getcolumnindex ("email"));
log.d ("Contact", "ID is" +id);
log.d ("Contact", "name is" +namestring);
log.d ("Contact", "phone is" +phonestring);
log.d ("Contact", "e-mail is" +emailstring);
} while (Cursor.movetonext ());
}
}
});
}
}
You can also manage the SQLite database by using the Sqlite3 command from the ADB tool in the Sdk\platform-tools directory.
The SQLite Foundation of Android Learning