To access the SQLite database, android must use the sqliteopenhelper class (in fact, it is not necessary, but it will be complicated to operate without sqliteopenhelper ).
Sqliteopenhelper is a helper class used to manage database creation and version upgrade.
The sqliteopenhelper class is an abstract class. You can create a databasehelper class in the program to inherit the sqliteopenhelper class and implement or rewrite some of its methods.
The following describes how to use the sqliteopenhelper class.
Getreadabledatabase () -------- get a readable database
Getwritabledatabase () -------- get a writable Database
Oncreate (sqlitedatabase dB) -------- create a database callback function, where we can execute the SQL statement for creating a table
Onopen (sqlitedatabase dB) -------- open the database callback function
Onupgrade (sqlitedatabase dB, int oldversion, int newversion) -------- update the database callback function. You can add a table to it or modify the table field name.
Close () -------- close the database
Databasehelper. Java
Import android. content. context; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqliteopenhelper; import android. database. SQLite. sqlitedatabase. cursorfactory; // databasehelper serves as an Assistant class for accessing SQLite. It provides two functions: // first, getreadabledatabase () and getwritabledatabase () to obtain the sqlitedatabse object, this object can be used to operate the database. // Second, The oncreate () and onupgrade () callback functions are provided, allowing us to create and upgrade the database, perform your own operations on public class databasehelper extends sqliteopenhelper {Private Static final int version = 1; // In the subclass of sqliteoepnhelper, this constructor public databasehelper (context, string name, cursorfactory factory, int version) {// The constructor super (context, name, factory, Version) in the parent class must be called through Super );} // This is a constructor with three parameters. It calls the constructor public databasehelper (context, string name, int version) {This (context, name, null, Version) ;}// this is a constructor with two parameters. It calls the constructor public databasehelper (context, string name) {This (context, name, version);} // This function is executed when the database is created for the first time. It is actually the first time you get the sqlitedatabse object, this method will be called. // The first time you create a database is the first time you call the getreadabledatabase () or getwritabledatabase () method @ overridepublic void oncreate (sqlitedatabase dB) {system. out. println ("Create a database"); // execsqlstatement is used to execute SQL statements db.exe csql ("CREATE TABLE user (ID int, name varchar (20)");} @ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {system. out. println ("update a database ");}}
After writing databasehelper, use the sqlitedatabase object obtained by getwritabledatabase () or getreadabledatabase () to add, delete, modify, and query data.
Sqliteactivity. Java
Import Mars. sqlite3.db. databasehelper; import android. app. activity; import android. content. contentvalues; import android. database. cursor; import android. database. SQLite. sqlitedatabase; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class sqliteactivity extends activity {private button createdatabasebutton; private button UPDA Tedatabasebutton; private button insertbutton; private button updatebutton; private button querybutton;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // obtain the createdatabasebutton = (button) findviewbyid (R. id. createdatabase); updatedatabasebutton = (button) findviewbyid (R. I D. updatedatabase); insertbutton = (button) findviewbyid (R. id. insert); updatebutton = (button) findviewbyid (R. id. update); querybutton = (button) findviewbyid (R. id. query); // Add the listener createdatabasebutton for each button. setonclicklistener (New createdatabaselistener (); updatedatabasebutton. setonclicklistener (New updatedatabaselistener (); insertbutton. setonclicklistener (New insertlistener (); updatebutton. setonclickl Istener (New updatelistener (); querybutton. setonclicklistener (New querylistener ();} class implements onclicklistener {@ overridepublic void onclick (view v) {// create a databasehelper object databasehelper dbhelper = new databasehelper (sqliteactivity. this, "test_mars_db"); // It is created only after the getreadabledatabase () method of the databasehelper object is called or the getwritabledatabase () method, or a database sqlitedatabase DB = dbhelpe is opened. R. getreadabledatabase () ;}} class updatedatabaselistener implements onclicklistener {@ overridepublic void onclick (view v) {// create a databasehelper object, and the input version number is 2 (note that the input version number must be greater than the previous version number) databasehelper dbhelper = new databasehelper (sqliteactivity. this, "test_mars_db", 2); // After the getreadabledatabase () method or the getwritabledatabase () method is called, The callback function onupgradesqlitedatabase DB = dbhelper is called. getreadabledatabase () ;}} clas S insertlistener implements onclicklistener {@ overridepublic void onclick (view v) {// generate the contentvalues object contentvalues values = new contentvalues (); // you want to insert a key-value pair to this object, the key column is the column name, and the value is the value to be inserted into this column. The value must be consistent with the data type values in the database. put ("ID", 1); values. put ("name", "zhangsan"); databasehelper dbhelper = new databasehelper (sqliteactivity. this, "test_mars_db", 2); sqlitedatabase DB = dbhelper. getwritabledatabase (); // call the insert method. Insert data into the database. insert ("user", null, values) ;}} class updatelistener implements onclicklistener {@ overridepublic void onclick (view arg0) {contentvalues values = new contentvalues (); values. put ("name", "zhangsanfeng"); databasehelper dbhelper = new databasehelper (sqliteactivity. this, "test_mars_db"); sqlitedatabase DB = dbhelper. getwritabledatabase (); // update user set name = 'hangsanfeng 'Where id = '1' DB. Update ("user", values, "id =? ", New string [] {" 1 "}) ;}} class querylistener implements onclicklistener {@ overridepublic void onclick (view v) {databasehelper dbhelper = new databasehelper (sqliteactivity. this, "test_mars_db"); sqlitedatabase DB = dbhelper. getreadabledatabase (); cursor = dB. query ("user", new string [] {"ID", "name"}, "id =? ", New string [] {" 1 "}, null); While (cursor. movetonext () {string name = cursor. getstring (cursor. getcolumnindex ("name"); system. out. println ("query --->" + name );}}}}