The Android system has built-in support for the SQLite database and has provided help class Sqliteopenhelper. In development we need to create a new class to inherit Sqliteopenhelper and override the OnCreate and OnUpdate methods. The OnCreate method is only called the first time a new database is created, primarily as a table-creation operation. The OnUpdate method is called when the database version is updated, and some table creation, deletion, table structure changes are made according to the business needs. You also need to implement a constructor to pass in the context object, database name, cursor factory, and version number to the parent class's constructor. The following is a simple example:
1 Public classMysqlitehelperextendsSqliteopenhelper {2 PublicMysqlitehelper (Context context) {3 //Create a test.db database with a version number of 14 Super(Context, "test.db",NULL, 1);5 }6 7 @Override8 Public voidonCreate (Sqlitedatabase db) {9 //CREATE TABLE personTenDb.execsql ("CREATE TABLE person (_id Integer primary key AutoIncrement," + One"Name varchar (ten), Age Integer)"); A } - - @Override the Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { - //Delete the table person, and then create a new person table -Db.execsql ("Delete table if exists person"); -Db.execsql ("CREATE TABLE person (_id Integer primary key, name text)"); + } -}
Using SQL statements for incremental deletion and checking operations
Adding and deleting the operation using method Sqlitedatabase.execsql (). The query operation uses the method Sqlitedatabase.execsql (), which returns a cursor object that needs to be looped out of the data. The following is a sample code that uses an SQL statement to perform an add-and-scan operation:
1 //1. Create a new Mysqlitehelper Help class2Mysqlitehelper Mysqlitehelper =NewMysqlitehelper (mainactivity. This);3 //2. Get an example of a sqlitedatabase class4Sqlitedatabase Database =mysqlitehelper.getwritabledatabase ();5 //3. Inserting Data6Database.execsql ("INSERT into the person values (1, ' Zhang San ', 10)");7Database.execsql ("INSERT into the person values (2, ' John Doe ', 20)");8 //4. Modify the Data9Database.execsql ("Update person set age=30 where Name= ' Zhang San '");Ten //5. Delete Data OneDatabase.execsql ("Delete from person where Name= ' Zhang San '"); A //6, query all data, and stored in a list -cursor cursor = database.rawquery ("Select * from the person where age>?",Newstring[]{"10"}); -list<map<string, object>> resultlist =NewArraylist<> ();//Store the results of the query the while(Cursor.movetonext ()) {//Cyclic data fetching -map<string, object> map =NewHashmap<>(); -Map.put ("_id", Cursor.getint (Cursor.getcolumnindex ("_id")))); -Map.put ("Name", Cursor.getstring (Cursor.getcolumnindex ("name")))); +Map.put ("Age", Cursor.getint (The Cursor.getcolumnindex ("Age"))); - resultlist.add (map); + } ACursor.close ();//Close Cursors atDatabase.close ();//Close the database
Use the API to make additional deletions and check operations
The Sqlitedatabase class provides APIs for adding and removing changes, avoiding writing SQL statements directly in the code, and corresponding methods are insert, UPDATE, delete, query. The following is a simple example code:
1 //1. Create a new Mysqlitehelper Help class2Mysqlitehelper Mysqlitehelper =NewMysqlitehelper (mainactivity. This);3 //2. Get an example of a sqlitedatabase class4Sqlitedatabase Database =mysqlitehelper.getwritabledatabase ();5 //3. Inserting Data6Contentvalues person =Newcontentvalues ();7Person.put ("name", "Zhang San");8Person.put ("Age", 33);9 LongResultcreate = Database.insert ("Person",NULL, person);//Resultcreate greater than 0 indicates successful insertionTen //4. Update Data OneContentvalues Newperson =Newcontentvalues (); ANewperson.put ("Age", 55); - intResultupdate = database.update ("Person", Newperson, "Name=?",Newstring[]{"Zhang San"}); - //5. Delete Data the intResultdelete = Database.delete ("Person", "name=?",Newstring[]{"Zhang San"}); - //6. Query Data - //get cursor after the operation and use SQL statement query consistent, omitted here ... -cursor cursor = database.query ("Person",NULL,NULL,NULL,NULL,NULL,NULL); +Cursor.close ();//Close Cursors -Database.close ();//Close the database
Using SQLite in Android