Basic use of SQLite database

Source: Internet
Author: User
Tags sqlite sqlite database

The use of small databases built into Android is basically consistent with the use of SQL sever;

The basic process of creating a SQLite data is as follows, which requires implementation of the Sqlopenhelper database Management assistant interface. As follows: Create a new Mysqliteopebhelper class

1  Public classMysqlitehelperextendsSqliteopenhelper {2     //construct a Sqliteopenhelper3      PublicMysqlitehelper (context context, String name, Sqlitedatabase.cursorfactory factory,intversion) {4         Super(context,name,factory,version);5     }6 7 @Override8      Public voidonCreate (Sqlitedatabase db) {9         //Create a database and table (Id,name,age), where the primary key is ID, the self-increment formTenDb.execsql ("CREATE TABLE MyTable (ID integer primary key autoincrement,name text,age integer)"); One     } A  - @Override -      Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { the         //Database Updates -     } -}
View Code

Instantiate a database management helper instance in the activity, and then create a DB instance. Getreadabledatabase () and getwirtabledatabase () can create a sqlitedatabase, but two methods are different, the former creates a database that opens the database in read-write mode, and when the disk is full, Will fail, reopening will open the database in read-only mode, which creates a database that is open to read and write, and fails when the disk is full, so it is generally recommended to use the Getreadabledatabase () method to create the database. The following example lists the common methods of the database.

1Mysqlitehelper Mysqlitehelper;//declare a Database management helper Object2Sqlitedatabase database;//declare a Database object3 4     //constructs a Database management helper Object5Mysqlitehelper=NewMysqlitehelper ( This, "TestDB",NULL, 1);6     //This method creates a database that can read and write, the disk is full and will automatically change mode to read-only mode, getwritabledatabase () Full error7Database=mysqlitehelper.getreadabledatabase ();8     //Create a data table9Database.execsql ("CREATE TABLE MyTable (ID integer primary key autoincrement,name text,age integer)");Ten     //Add data (first method) OneContentvalues contentvalues=Newcontentvalues (); AContentvalues.put ("id", 1); -Contentvalues.put ("name", "Old King next Door"); -Contentvalues.put ("Age", 30);; theDatabase.insert ("MyTable",NULL, contentvalues); -     //Add data (method two) -String intsert= "Intsert into MyTable (id,name,age) value (1,\" next door Lao Wang ", 30)"; - Database.execsql (intsert); +     //Delete data (delete table Mytable,id=1 series data from that line) (method one) -Database.delete ("MyTable", "id=?",Newstring[]{"1"}); +     //Delete Data 9 method two) AString delete= "Delete from mytable where id=2"; atDatabase.execsql (delete);//Execute SQL statement -     //Query data (returns a pointer index to invoke the corresponding method to query the data) -Cursor cursor=database.query ("MyTable",Newstring[]{"id", "name", "Age"},NULL,NULL,NULL,NULL,NULL); -      while(Cursor.movetonext ()) { -         intIdindex=cursor.getcolumnindex ("id"); -         intId=Cursor.getint (idindex); in  -         intNameindex=cursor.getcolumnindex ("name"); toString name=cursor.getstring (nameindex); +  -         intAgeindex=cursor.getcolumnindex ("Age"); the         intAge=Cursor.getint (ageindex); *  $String result=id+ "" +name+ "" +age+ "\ n";Panax Notoginseng     } -     //Modifying Data theContentvalues contentvalues1=Newcontentvalues (); +Contentvalues1.put ("id", 3); AContentvalues1.put ("name", "Little King Next Door"); theContentvalues1.put ("Age", 15); +Database.update ("MyTable", ContentValues1, "id=?",Newstring[]{"1"}); -     //Close the database $Database.close ();
View Code

Basic use of SQLite database

Related Article

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.