Android Database Summary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.