Android SQLite-based usage

Source: Internet
Author: User

On the Android platform, an embedded relational database --SQLiteIt supports NULL, INTEGER, REAL (floating point number), TEXT (string TEXT), and BLOB (binary object) data types, although only five data types are supported, in fact, data types such as varchar (n), char (n), and decimal (p, s) can be converted to the corresponding five data types during operation or storage.
Ex: You can store strings in Integer fields, floating point numbers in Boolean fields, or date,! The field defined as integer primary key can only store 64-bit integers. In addition, when writing the create table statement, you can omit the Data Type following the field name;
Create table person (personid integer primary key autoincrement, name varchar (20) // The type of name can be omitted;
Select * from tablename where tiaojian group by grouping words having... order by sorting words
Select * from person
Select * from person order by id desc/asc
Select name from person group by name having count (*)> 1
Paging: select * from Account limit 5 offset 3 or select * from Account limit3, 5
Insert statement: insert into tablename (Field List) values (Value List)
Insert into person (name, age) values ("Livingstone", 22)
Update statement: update tablename set field1 = val1, field2 = val2 where Condition Statement
Update person set name = "Livingstone" where id = 10
Delete statement: delete from tablename where Condition Statement
Delete from person where id = 10
Obtain the auto-increment ID value after adding a record: select last_insert_rowid ()

SQLiteDatabase provides operation methods for adding, deleting, updating, and querying: insert (), delete (), update (), and query ();

The Insert () method is used to add data. Data in each field is stored using ContentValues. ContentValues is similar to MAP. Compared with MAP, it provides put (String key, Xxx value) for data access) and getAsXxx (String key) methods;
Long rowid = db. insert ("person", null, values); // return the row number of the newly added record, regardless of the primary key id;
No matter whether the third parameter contains data, the Insert () method must add a record. If the third parameter is null, a record other than the primary key will be added.
For records with Null field values, the Insert () method is used to add data by constructing an SQL statement. The second parameter is used to specify the name of the Null field: if the value of the third parameter is Null or the number of elements is 0, the Insert () method requires that you add a record with Null values for fields other than the primary key. To meet the SQL syntax requirements, the insert statement must be given a field name, ex: insert into person (name) values (NULL). If the field name is not given, the insert statement is: insert into person () values (), for field names, we recommend that you use a field other than the primary key. If an INTEGER type primary key field is used, after executing an insert statement similar to insert into person (personid) values (NULL, the value of this primary key field is not NULL. If the value of the third parameter is not Null and the number of elements is greater than 0, the second parameter is set to null.

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.