SQLite usage of Android data storage

SQLite is an open source, embedded relational database. The first version of alpha was released in 2000. SQLite has outstanding performance in portability, ease of use, compactness, efficiency and reliability.

The SQLite database created in Android is stored in: / data / data / < package name > / databases /.

Main features:

-Lightweight

-Independence, no dependency, no installation

-Cross platform, supporting many operating systems

-Supports databases up to 2TB in size

-Each database exists as a single file

-It is stored on the hard disk in the form of b-tree data structure

Data type of SQLite:

SQLite supports null, integer, real, text, and BLOB data types

They represent: null value, integer value, floating point value, string value and binary object.

Dynamic data type (weak reference):

When a value is inserted into the database, SQLite will detect its data type. If the type does not match the associated column, SQLite will try to convert the value to the type of the column. If it cannot be converted, the value will be stored as its own type.

Using SQLite in Android mainly involves two classes:

Sqlitedatabase and sqliteopenhelper are mainly analyzed below.

sqliteDatabase

This class provides some methods for managing SQLite databases, such as creating, deleting, executing SQL commands, and performing other common database management tasks. The database name of each program is unique.

Common methods:

DB. Execsql (string SQL) / / execute any SQL statement

DB. Insert (string table, string nullcolumnhack, contentvalues) / / insert record

DB. Delete (string table, string whereclause, string [] whereargs) / / delete records

DB. Update (string table, contentvalues, string [] whereargs) / / update the record

DB. Query (string table, string [] columns, string selection, string [] selectionargs, string groupby, string having, string orderby) / / query records

Db.rawquery (string SQL, string [] selectionargs) / / query records through SQL statements

The following is an example of simple operation of SQLite database:

After openorcreatedatabase ("user. DB", null) is executed, a database file will be created in / data / data / < package name > / databases / directory. Open DDMS to view it. You can also export it and use tools such as navigator to open it to view the data in it.

In addition, the above example uses the execsql () method to operate records through native SQL statements. Of course, you can also use the common methods of sqlitedatabase described above, such as insert (), delete (), update (), query (), etc. However, it should be noted that when the amount of data is small, the efficiency of inserting records using SQL statements through execsql () is similar to that using insert () method. However, if the amount of data is large, the efficiency of using the former is much higher than that of using the latter.

sqliteOpenHelper

This class is the help class of sqlitedatabase, which is mainly used to manage database creation and version update. Sqlitehelper is an abstract class, which is generally used by creating a subclass inherited from it and overriding oncreat () and onupgrade () methods.

-Oncreat (sqlitedatabase dB) / / called when creating a database for the first time. It is generally used for operations such as creating tables.

-Onupgrade (sqlitedatabase dB, int OldVersion, int newversion) / / called when upgrading the database version

Here is a simple example of using sqliteopenhelper:

Create a subclass that inherits from sqliteopenhelper

Then you can obtain an sqlitedatabase object through the instance of sqlitehelper, and then perform a series of operations on the database.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>