Data storage Method-sqlite

Source: Internet
Author: User
Tags sqlite

SQLite is an open source embedded database engine written in C language. Sql92biaozhun is supported by too many, and can be run on all major operating systems.
    • Supports databases up to 2TB in size
    • In the form of a single file
    • stored in disk in the form of a B-TREE data structure

Characteristics:
    1. Lightweight a dynamic library, single file
    2. Independence is not dependent, installation is not required
    3. Isolation is all in one folder
    4. Multiple operating systems supported across platforms
    5. Multi-lingual interface supports many programming languages
    6. Security transactions

Security issues with Transaction handling:
    • Independent transactions with exclusive and shared locks on the database
    • Multiple processes can read data from the same database at the same time, but only one can write to the data

Data type of SQLite:
    • NULL, Integr, REAL, text, and BLOB data types
    • Null, shaped, floating-point, string value, binary value
Dynamic Data type (weak reference) when a value is inserted into the database, SQLite checks its type, and if the type does not match the associated column, SQLite attempts to convert the value to the type of the column, and if it cannot, the value is stored as its own type.

Sqlitedatabase: Provides some of the classes that manage SQLite databases to provide methods for creating, deleting, executing SQL commands, and performing other common database management tasks each program's database name is unique
    • Execsql (SQL)
    • Insert (table,nullcolumnhack,values)
    • Delete (Table,whereclause,whereargs)
    • Update (Table,values,whereclause,whereargs)
    • Query (Table,columns,selection,selectionargs,groupby,having,orderby)
    • Rawquery (Sql,selectionargs)

Example:--------------------------------------------------------------------------------------------// Each program has its own database, under normal circumstances do not interfere with
Create a database and open
Sqlitedatabase db = Openorcreatedatabase ("User.db", mode_private, NULL);
Db.execsql ("CREATE table if not exites USERTB (_id integer primary key autoincrement,name text not null,age integer not nul L,sex text NOT NULL) ");
Db.execsql ("INSERT into USERTB (name,sex,age) VALUES (' Zhang San ', ' Male ', ' 18 ')");
Db.execsql ("INSERT into USERTB (name,sex,age) VALUES (' John Doe ', ' female ', ' 19 ')");
Db.execsql ("INSERT into USERTB (name,sex,age) VALUES (' Harry ', ' Male ', ' 20 ')");
Cursor c=db.rawquery ("Select * Form USERTB", null);
if (c!=null)
{
while (C.movetonext ())
{
LOG.I ("info", "" "+c.getint (C.getcolumnindex (" _id "));
LOG.I ("info", "" "+c.getstring (C.getcolumnindex (" name "));
LOG.I ("info", "" "+c.getint (C.getcolumnindex (" Age "));
LOG.I ("info", "" "+c.getstring (C.getcolumnindex (" Sex "));
}
C.close ();
}
Db.close ();---------------------------------------------------------------------------------------------------- -----------

Contentvalues:
    • Used to store a set of values that can be contentresolver processed.
    • Contentvalues value = Newcontentvalues ();
      • Similar HashMap key value
    • Values.put ("Key", "values");
Example:------------------------------------------------------------------------------------------------------- --------sqlitedatabase db= openorcreatedatabase ("Stu.db", mode_private, NULL);
Db.execsql ("CREATE table if not exists STUTB (_id integer primary key autoincrement,name text not null,sex text not Null,ag e integet NOT null) ");
Contentvalues values = new Contentvalues ();
Values.put ("name", "Wang Nima");
Values.put ("Sex", "male");
Values.put ("Age", 20);
Db.insert ("Stutb", null, values);
Values.clear ();
Values.put ("name", "Linima");
Values.put ("Sex", "male");
Values.put ("Age", 22);
Db.insert ("Stutb", null, values);
Values.clear ();
Values.put ("Sex", "female");
Db.update ("Stutb", Values, "_ID>?", New string[]{"0"});
Db.delete ("Stutb", "Name like", New string[]{"% King"});
Cursor c =db.query ("Stutb", NULL, "_ID>?", New string[]{"0"}, NULL, NULL, "name");
if (c!=null)
{
String []columns= c.getcolumnnames ();
while (C.movetonext ())
{
for (String columnname:columns)
{
LOG.I ("Info", C.getstring (C.getcolumnindex (ColumnName)));
}
}
}
}-------------------------------------------------------------------------------------------------------------- -----------------------------Sqliteopenhelper:
    • Sqlitedatabase help class for managing database creation and version updates
    • The general usage is to establish a class to inherit it, and override the OnCreate () and Onupgrade () methods
    • OnCreate (Sqlitedatabase db) creates a database call,
    • Inupgrade (sqlitedatabase db,int oldversion,int newversion) is called when the version is updated
    • Getreadabledatabase () Create or open a read-only database
    • Getwritedatabase () Create or open a read-write database




Data storage Method-sqlite

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.