About the features and concepts of SQLite database in Android and how to use it!

Source: Internet
Author: User


2. Process of database Operation
Prepare database System (MySQL)--Create database--Create/Design data table (table)--Data operations
Data Operations Connection Database (Connection)--Prepare SQL statement (String SQL)---Execute SQL statement (statement/preparedstatement)--and process results (int/ ResultSet)




3. Using SQLite
1) Create a database
Openorcreatedatabase (String name, int mode, Cursorfactory Factory)
String Name: Database file name, such as users.db, can be used without forcing the extension of database files.
int mode: Access mode, constants should be used.
Cursorfactoty Factory: Cursor factory, usually using NULL.
After the database is created successfully, it is saved under the/data/data/package name/databases/


2) Create a data table
The return value of Openorcreatedatabase () is a Sqlitedatabase object that executes SQL instructions using the object's Execsql (String sql) method
CREATE TABLE Dictionary (_id INTEGER PRIMARY KEY autoincrement, en varchar () NOT NULL UNIQUE, zh varchar (+) not NULL);


3) Data manipulation
Use Execsql (String sql) to execute a single SQL syntax that does not have a variable, that is, it does not need to be precompiled, usually behaves as a data table-related operation, using Execsql (String sql, object[] bindargs) Used to perform operations related to the data itself.
The Execsql () method has no return value and is definitely not available for queries, nor is it recommended for adding, modifying, and deleting operations.
Android official recommended Execsql () is not used to perform any additions or deletions to the data operation.


4) Recognize Contentvalues class
In the data operation, use insert/update ... Method needs to use this type of data, which is essentially an operation HashMap, where key corresponds to the field name in the data table, and value corresponds to the data of the field whose operation


5) Insert record using Db.insert (string table, String nullcolumnhack, contentvalues values)
String table: Table name
String Nullcolumnhack: Required when the 3rd parameter values are null or no value (values.size==0) is complete for the security SQL statement
Contentvalues values: Configuring the Value of fields and fields that need to be manipulated
Return value: Returns 1 if an error occurs, otherwise returns the ID of the inserted record


6) Modify the record using db.update (table, values, Whereclause, Whereargs)
String table: Table name
Contentvalues values: Ibid.
Whereclause:where clause, do not add a "where" character, the parameter value such as "_id=?" or (Username= and password=?) "
The arguments in the Whereargs:where clause should match the question mark in the Whereclause
Return value: Number of rows affected


7) Delete records using Db.delete (string table, String Whereclause, string[] whereargs)
Description of each parameter ibid.
Return value: Number of rows affected


8) Use Db.query (string table, string[] columns, string selection, string[] Selectionargs, String groupBy, String having, string by, string limit) query record
String table: Table name
string[] Columns: Field List
String selection: See previous Whereclause
String[] Selectionargs: See previous Whereargs
String Groupby:group by (group) clause
String having:having clause
String Orderby:order by (sort) clause
String limit:limit (pagination) clause


9) Understanding the cursor
ResultSet interface similar to JDBC
Master the Getxxxx () and movexxxx () methods


10) Know Sqliteopenhelper
Parameters of the constructor method
Context Context: Contextual Object
String Name: Database name, file name, such as Tarena.db
Cursorfactory Factory: Cursor factory, value NULL
int version: Current version, not less than the same app's historical version number

OnCreate (): Execute when database is created
Onupgrade (): Database version upgrade is performed

Getreadabledatabase () and Getwritabledatabase (), where Getreadabledatabase () will attempt to acquire a writable sqlitedatabase, but when the storage space is low, will return read-only sqlitedatabase to avoid program errors.


One ) Simplecursoradapter
Use the cursor as the simpleadapter of the data source, where the cursor must contain _id fields, parameter string[] from the value of ColumnName in the cursor



* * char and varchar in the database
Char is fixed length, if the length of the string is less than the length of the contract, then the white space on the right side of the original character, for example, char (8) in the "XYZ" will behave as "xyz", varchar is longer, length is determined by the string itself.

* * SQLite is a database with less stringent data type requirementsany field can accept data of any data type, but it is not recommended to fill the data arbitrarily

about the features and concepts of the SQLite database in Android and how to use it!

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.