Android integrates SQLite at runtime, so each Android application can use the SQLite database.
We manipulate the SQLite database by Sqlitedatabase objects of this class and do not require authentication.
Where the database resides:data/< the project folder >/databases/
Public classMainactivityextendsActivity {PrivateButton CreateDatabase, CreateTable, Selectdata, InsertData, UpdateData, Deletedaata; PrivateMysqliteopenhelper Helper; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.demo); //Initialize buttonCreateDatabase =(Button) Findviewbyid (r.id.createdatabase); CreateTable=(Button) Findviewbyid (r.id.createtable); Selectdata=(Button) Findviewbyid (r.id.selectdata); InsertData=(Button) Findviewbyid (r.id.insertdata); UpdateData=(Button) Findviewbyid (r.id.updatedata); Deletedaata=(Button) Findviewbyid (r.id.deletedaata); Helper=NewMysqliteopenhelper (mainactivity. This, "Mydb.db", NULL, 1); /** CREATE DATABASE*/Createdatabase.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getwritabledatabase (); Toast.maketext (mainactivity. This, "Database creation succeeded", 1000). Show (); } }); /** Create a table*/Createtable.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View arg0) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getwritabledatabase (); //Create a tablesqlitedatabase. Execsql ("CREATE TABLE student (ID INTEGER PRIMARY KEY autoincrement,name text)"); Toast.maketext (mainactivity. This, "Table creation succeeded", 1000). Show (); } }); /** Insert Data*/Insertdata.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getwritabledatabase (); Sqlitedatabase. Execsql ("INSERT into student (name) VALUES (' tinyphp ')"); Toast.maketext (mainactivity. This, "Insert data Success", 1000). Show (); } }); /** Update Data*/Updatedata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getwritabledatabase (); Sqlitedatabase. Execsql ("Update student set Name= ' monkey ' where id= ' 1 '"); Toast.maketext (mainactivity. This, "Data Update succeeded", 1000). Show (); } }); /** Query Data*/Selectdata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getreadabledatabase (); //Parameters:table name, query columns, query criteria, query parameters, query result grouping, grouping result limits, sortingcursor cursor = sqlitedatabase.query ("Student",Newstring[] {"id", "name"}, "Id=?",NewString[] {"1"},NULL, NULL,NULL); String name=NULL; while(Cursor.movetonext ()) {name=cursor.getstring (Cursor.getcolumnindex ("name")); } toast.maketext (mainactivity. This, "The query result name is:" +name, 1000). Show (); } }); /** Delete Data **/Deletedaata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getwritabledatabase (); Sqlitedatabase.delete ("Student", "id=?",Newstring[]{"1"}); Toast.maketext (mainactivity. This, "Data deletion succeeded", 1000). Show (); } }); }}
Example Download >>>>>>>>
Related articles:
Android: The sharepreferences of storage mode
Android: Data Storage SQLite