Android SQLite Learning

Source: Internet
Author: User
I. Introduction to SQLite

On the Android platform, an embedded relational database named SQLite and sqlite3 is integrated.Supported
Null, integer, real (floating point number), text (string text), blob (binary object) data type,
Although it supports only five types
Sqlite3It also accepts data types such as varchar (N), char (n), and decimal (P, S), but converts them to the corresponding five data types during operation or storage.

The biggest feature of SQLite is that you can save any type of data to any field, regardless of the Data Type declared in this column. For example, you can store strings in integer fields or
The Type field contains floating point numbers, or the type field contains the date value. One exception is that fields defined as integer primary key can only store 64-bit integers,
An error occurs when data other than integers is stored in this field. In addition, SQLite ignores the create table statement when parsing the create table statement.
The data type information following the field name in the table statement.

Ii. curd of SQLite

Android providesSqlitedatabase class, which encapsulates APIs for database operations. You can use this class to add data.
(Create), retrieve, update, and delete (crud) Operations)
. Pair
Sqlitedatabase learning should be focused onExecsql () and rawquery () Methods
.
Execsql () can be used to execute SQL statements with changed behaviors such as insert, delete, update, and create table;
The rawquery () method can execute the SELECT statement. Sqlitedatabase also provides operations corresponding to adding, deleting, updating, and querying
Method: insert (), delete (), update (), and query ()
. These methods are actually used by cainiao who are not familiar with SQL syntax.ProgramThe execsql () and rawquery () methods are directly used for execution.
SQL statements can be used to add, delete, update, and query data.

Iii. SQLite Transaction Management

Use sqlitedatabaseThe begintransaction () method can start a transaction.
, The program executes to endtransaction ()
Method will check whether the transaction flag is successful. If it is successful, commit the transaction; otherwise, roll back the transaction. When the application needs to submit transactions,The endtransaction () method must be executed in the program
Use settransactionsuccessful ()
Method To set the transaction flag to successful,
If you do not call the settransactionsuccessful () method, the transaction is rolled back by default.

Iii. create and update data tables using SQLite

If the application uses the SQLite database, you need to create the database table structure used by the application and add some initialization records when using the software for the first time. In addition, when the software is upgraded
The data table structure needs to be updated. In the Android systemSqliteopenhelper class, which is used to manage database versions,
This class is
Abstract classes, which must be inherited before they can be used. To manage database versions, the sqliteopenhelper class has two important methods:
Oncreate (sqlitedatabase dB) and onupgrade (sqlitedatabase dB, int oldversion,
Int newversion)

When the getwritabledatabase () or getreadabledatabase () method of sqliteopenhelper is called
When operating the sqlitedatabase instance of the database, if the database does not exist, the android system will automatically generate a database and then call oncreate ()
The oncreate () method is called only when the database is generated for the first time. In the oncreate () method, the database table structure can be generated and the initialization data used by some applications can be added.
The onupgrade () method is called when the database version changes. The database version is controlled by the programmer. Assuming that the current database version is 1, the database is modified due to business needs.
The table structure. At this time, you need to upgrade the software. When upgrading the software, you want to update the database table structure on your mobile phone, you can set the original database version to 2 (or another value) and
Update the table structure in the onupgrade () method. When the number of software version upgrades is large, the onupgrade () method can be used based on the original number and target version number.
Then, update the table structure and data.

Both the getwritabledatabase () and getreadabledatabase () methods can obtain
Sqlitedatabase instance. However, getwritabledatabase ()
Method to open the database in read/write mode. Once the disk space of the database is full, the database can only read but cannot write data. If getwritabledatabase () is used ()
An error occurs. The getreadabledatabase () method first opens the database in read/write mode. If the disk space of the database is full, it will fail to be opened.
Open the database in read-only mode.

From: http://www.blogjava.net/changcheng/archive/2010/03/01/314214.html

Sqlite3 viewing tool, command line:

All database files are stored in the/data/package_name/databases path on the mobile phone. The most important thing is:
In Android, the data of all applications (including files and databases) is private to the application. To share data with different applications, you must use
Contentprovider implementation, a contentprovider class implements a set of standard method interfaces, so that other applications can save or read this
Various data types of contentprovider.

To facilitate testing, how can I view the content of the SQLite database? Use the tool sqlite3.exe provided by the android SDK. This is a command line utility that lists the main Commands used: view the table structure sqlite3 "path". Dump

For example, G:/Android-SDK-window/tools> sqlite3 30m. db3. Bump,

The running structure is as follows: beggin transaction;

Create Table [tmprogramm] ([Index] integer, [NID] integer, [nchannelid] integer, [strtitle] char (255 ),

[Dwduration] integer, [strthumbnail] char (1024), [strurl] char (255), [strpathname] char (255 ),

[Strauthor] char (255), [dwsize] integer, [State] integer, [percent] integer,

[Strconverted] char (255), [dwvideotype] integer, [dwdownloadedbytes] integer, [strtime] char (255 ),

[Strhttpversion] char (255), [struseragent] char (255), [strreferer] char (255 ));

Commit;

This is the statement used to create the database.

View sqlite3 "path". schema in the table

Reprint: http://www.moandroid.com /? Page_id = 1176

Reprinted: http://www.cnblogs.com/nethome/archive/2010/06/10/1755661.html


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.