Android Database Summary The first step: Write your own class inheritance Sqliteopenhelper, as follows
Package Com.example.wxj2048;import Android.content.contentvalues;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqliteopenhelper;public class Dbdao extends sqliteopenhelper{private static final String DBNAME = "Mldn.db";p rivate static final int dbversion = 1;private static final String tabname = "game2048";p ublic Dbdao (Co ntext context) {Super (context, DBNAME, NULL, dbversion);} @Overridepublic void OnCreate (Sqlitedatabase db) {//TODO auto-generated method Stubstring sql = "CREATE TABLE" + tabname+ "(" + "id integer," + "score Integer" + ")";d b.execsql (SQL);} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//TODO auto-generated method Stubstrin G sql = "DROP TABLE IF EXISTS" + tabname;db.execsql (SQL); this.oncreate (db);} public void Insert (Integer score) {Sqlitedatabase db = Getwritabledatabase (); Contentvalues values = new Contentvalues (); Values.put ("id", 1); valueS.put ("Score", score);d B.insert (tabname, NULL, values);d b.close (); public void Delete () {Sqlitedatabase db = Getwritabledatabase (); string[] data = new STRING[1]; Data[0] = "1";d b.delete (tabname, "id =?", data);d b.close ();} Public Integer query () {Sqlitedatabase db = Getwritabledatabase (); Cursor C = db.query (tabname, NULL, NULL, NULL, NULL, NULL, NULL); C.movetofirst (); int index = C.getcolumnindex ("score"); String result = c.getstring (index);d b.close (); return integer.valueof (Result);}}
The second step is to create a Dbdao object and then use the database
Let's summarize the important method of Cursor
Close ()
Close cursors, freeing resources
getColumnCount ()
Returns the total number of all columns
Getcolumnindex (String columnName)
Returns the name of the specified column if there is no return-1
getColumnName (int columnindex)
Returns the column name from the given index
Getcolumnnames ()
Returns the column name of a string array
GetCount ()
Returns the number of rows in the cursor
Movetofirst ()
Move cursor to First line
Movetolast ()
Move the cursor to the last line
MoveToNext ()
Move the cursor to the next line
Access the Cursor's subscript to get the data
int namecolumnindex = Cur.getcolumnindex (people.name);
String name = cur.getstring (Namecolumnindex);
Android Database Summary