Basic article: 6. Android Database Programming---SQLite

Source: Internet
Author: User

Brief introduction:

On the Android platform, an embedded relational database is inherited---sqlite.sqlite has cross-platform features that can be run on all major operating systems. SQLite implements independent transactions with exclusive and shared locks, so multiple processes can read data from the same database at the same time, but only one can write to the data, and an exclusive lock must be obtained before the write operation. SQLite, on the other hand, takes a dynamic data type, and when a value is inserted into the database, SQLite checks its type, and if the type does not match the associated column, SQLite casts it. SQLite supports the following data types: null (NULL), Integer (integer value), REAL (floating-point value), text (string literal), BLOB (binary object).

Sqliteopenhelper class:

  Sqliteopenhelper provides two important ways to do this:

  1.onCreate (sqlitedatabase db) when the application first creates a DB instance, a new database is created, followed by a call to the OnCreate () method, in which the database table structure is generated and some data initialization is done.

  2.onUpdate (sqlitedatabase db, int oldversion,int newversion) when the database version changes, the method is called, and some table structures are updated.

Sqlitedatabase class:

Some operations on the database, such as additions and deletions can be done through this class:

1.inser ()---additional data

//Add Data     Public voidinser (user user) {contentvalues values=NewContentvalues ();//Create a Contentvalues object//inserts a key-value pair into the Contentvalues object, the first parameter is the column name, and the second parameter is the column data valuevalues.put (USERNAME, User.getusername ());        Values.put (PASSWORD, User.getpassword ()); //call the Insert method to insert data into the database//first parameter: Table name//second argument: If Contentvalues is empty, the column is null (SQLite does not allow empty columns)Db.insert (TABLE_NAME,NULL, values); }

2. Delete()---deleting data

 //  delete data   void   Delete () {String whereclause  = "Username=?"; //         delete condition  String[] whereargs={"Mr"}; //  Remove the parameters of the condition  db.delete (table_name, whereclause, Whereargs); //  execute delete  //  The second deletion method  /*   String sql= "Delete fro M "+table_name+" where username= ' Mr ' ";//delete operation SQL statement db.execsql (SQL);//execute delete  */  

3.update ()---change data

//Change Data     Public voidUpdate () {String whereclause= "Username=?";//Delete ConditionString[] whereargs={"Mr"};//parameters for delete conditionContentvalues values=Newcontentvalues (); Values.put ("PASSWORD", "555");//Add a field to modifydb.update (table_name, values, Whereclause, Whereargs); //second method of change        /** String sql= "UPDATE [table_name] set password= ' 555 ' where username= ' Mr '"//Update SQL statement * DB.EXECSQL (SQL); * */    }

4. Query()---querying data

//querying data by ID     PublicUser Query (intID) {User User=NewUser (); //first parameter: Table name//second parameter: The name of the column to query//Third parameter: query criteria//Fourth parameter: parameters of the query condition//Fifth parameter: Group the results of a query//Sixth parameter: Limit the results of a grouping//Seventh parameter: Sort the results of a querycursor cursor = DB.QUERY (table_name,NewString[] {USERNAME, PASSWORD}, "_id =" + ID,NULL,NULL,NULL,NULL); if(Cursor.getcount () >0) {Cursor.movetofirst ();//move a cursor to the first recordUser.setusername (cursor.getstring (0));//The value of the user name is then set, "0" represents the index of the column, and Cursor.getcolumnindex ("username") is also availableUser.setpassword (cursor.getstring (1)); returnuser; } cursor.close ();//Close Cursors        return NULL; }

There are two ways to operate the database, one is for the rookie level like me to implement the corresponding operation by calling the Sqlitedatabase encapsulation method, and the other is to execute the SQL statement directly through DB.EXECSQL () to implement the specified operation.

A cursor class is involved in some operations on the database, and querying the data in Android is done through the cursor. The cursor has several features:

*cursor is a collection of each row

* Use Movetofirst () to navigate to the first line

* Must know the name and data type of each column

*cursor is a random source of data

* All data is obtained by subscript

Here are some common ways that cursor is used:

*close ()---close cursor

*getcolumnindex (String columnName)---Get the specified column index

*movetofirst ()---Move the cursor to the first line

*movetolast ()---Move the cursor to the last line

*movetonext ()---Move the cursor to the next line

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.