Brief introduction
Sqilte is a lightweight database, satisfying the basic operation of the database, due to the limited memory of the mobile side, so sqilte just can meet the basic requirements of mobile development.
Nonsense not much to say on the code
1. First introduce, SQLite management class Sqliteopenhelper. This class is used to manage the creation and release of a database, and it needs to inherit its methods.
The code is as follows:
Public class Db extends sqliteopenhelper { Public Db(context context, String name, Cursorfactory factory,intVersion) {Super(Context,"DB",NULL,1);//TODO auto-generated constructor stub}@Override Public void onCreate(Sqlitedatabase db) {//TODO auto-generated method stubDb.execsql ("CREATE TABLE User ("+"name TEXT DEFAULT \" \ ""+"Sex TEXT DEFAULT \" \ ")"); }@Override Public void Onupgrade(Sqlitedatabase DB,intOldversion,intNewVersion) {//TODO auto-generated method stub}}
Where OnCreate when the database is created callback, Onupgrade method is the version number is increased, the automatic callback, so the database to update the operation is written inside.
Inserting operations on a database
Public class mainactivity extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.main); DB db =NewDb ( This);There are two ways to get a database, which is to get read-write and to get a readable database. The Db.getreadabledatabase () method is obtained. Sqlitedatabase dbWriter = Db.getwritabledatabase (); Contentvalues CV =NewContentvalues (); Cv.put ("Name","Shangsan"); Cv.put ("Sex","Male");The first parameter indicates that the second represents an empty column, and the third represents the data to be filled in contentvalues encapsulation. //dbwriter.insert (table, nullcolumnhack, values);Dbwriter.insert ("User",NULL, CV); Db.close (); }}
Query operations
Public class mainactivity extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.main); DB db =NewDb ( This);//Sqlitedatabase DbWriter = Db.getwritabledatabase ();//contentvalues CV = new Contentvalues ();//Cv.put ("name", "Shangsan");//Cv.put ("Sex", "male");////dbwriter.insert (table, nullcolumnhack, values);// Sqlitedatabase dbraed = Db.getreadabledatabase ();//cursor c = dbraed.query (table, columns, selection, Selectionargs, GroupBy, having, by-and-by);Cursor C = Dbraed.query ("User",NULL,"Name=?",Newstring[]{"Zhangsan"},NULL,NULL,NULL); while(C.movetonext ()) {String name = c.getstring (C.getcolumnindex ("Name")); String sex = c.getstring (C.getcolumnindex ("Sex")); System.out.println (name+" "+sex); } }}
Android Database Sqlite-android learning Journey (ix)