The use of SQLite in the Android program, SQLite additions and deletions to find ways to change

Source: Internet
Author: User

Sqlite:

1. A Lightweight data management tool for local data storage is one of the many tools used to implement database management.

2.Android has absorbed the code functions of SQLite in its system, we can directly in the Android program using SQLite's statement to create a database, and to implement database additions and deletions to change

Simple ways to use SQLite:

How to get a class template for a database and set the data structure in the database:

Create yourself a new class, Inherit Sqliteopenhelper, and override the constructor method, OnCreate (), Onupgrade ()

The code implementation method for the custom class and the original template parsing of the construction method:

 public  class  Database extends   Sqliteopenhelper { public  Database (Context context, String name, Sqlitedatabase.cursorfactory factory,   version) { super   (context, name, Factory, version); 
//context
//string name "CREATE database name"
// sqlitedatabase.cursorfactory Factory "Find the cursor in the database (phase As subscript in the array) "
///INT Version" database "
    }    @Override    publicvoid  onCreate (sqlitedatabase db) {// is called when the database is created     }    @Override    publicvoidint int  NewVersion) {// each time the database is started detects the version information of the database and executes this method if there is a change in the version    }}

Specific code to implement the custom class of the database:

Public class Data extends sqliteopenhelper

 PublicData (Context context) {

    For simplicity, the database is directly named data, the cursor information is NULL, and the version is 1
Therefore, only the context parameter is used in the constructor of a custom class
When calling the parent class construction method, a fixed parameter is given to simplify database creation
Super(Context, "DATA",NULL, 1); } @Override Public voidonCreate (Sqlitedatabase db) {
      //Create a statement of the database construction content, specifically parsing the db.execsql ("CREATE TABLE User (" +
"_id INTEGER PRIMARY KEY autoincrement," + "Name text default \" \ "," + "sex TEXT default \" \ ")");
    The above statement gives a database structure object named User, containing the Interger type of _id information, and self-increment
The name of the text data type
The text data type of sex
} @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { //Because the version parameter information is given a fixed 1, the database update operation is not performed }}

After rewriting the template of the database, how to implement the database usage and management:

1. Use a custom database class to create a new database object,

2. The database object and the writable database object are obtained through the new database.

The new Contentvalues object adds content to the obtained writable database, Implementation code:

New Data (this); Private Sqlitedatabase dbWriter =new  contentvalues (); Cv.put ("name", Etname.gettext (). ToString ()); Cv.put ("Sex", Etsex.gettext (). toString ());
The meanings of the three parameters of the Insert method are: Insert the name of the data table, insert the location information of the data table, insert the object
Dbwriter.insert ("user",null, CV);
After each use of the database, remember to close the database, similar to I/O
Dbwrite.close ();


Note: Location information is filled here is null, the specific operation is not clear, but when you see here, there is time to study

By obtaining a readable database object, the method implementing the lookup in the target database is implemented with code:

Sqlitedatabase DB = db.getreadabledatabase ();
Creates a new cursor object that indicates the lookup in the "User" data table, followed by a series of constraints,
= Db.query ("user",null,null,null, NULL,null,null);
Perform a read operation of the database when the cursor is still able to run down a line
while (C.movetonext ()) {String name=c.getstring (C.getcolumnindex ("name")); String sex=c.getstring (c.getcolumnindex ("Sex")); Toast.maketext (Getapplicationcontext (), name+ "--" +sex,toast.length_short). Show ();
}

The use of SQLite in the Android program, SQLite additions and deletions to find ways to change

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.