Embedded relational database-SQLite,
Sqlite3 supports null, integer, real (floating point number), text (string text), and blob (binary object) data types,
Although it supports only five types, sqlite3 actually accepts data types such as varchar (N), char (N), decimal (P, S, it is only converted to the corresponding five data types during operation or storage.
Create Table person (personid integer primary key autoincrement, name varchar (20 ))
SQLite can parse most standard SQL statements, such:
Query statement: Select * from table name where Condition Clause group by grouping clause having... order by sorting clause
For example, select * From person
Select * From person order by ID DESC
Select name from person group by name having count (*)> 1
Paging SQL is similar to MySQL. The following SQL statement obtains five records and skips the first three records.
Select * from account limit 5 offset 3 or select * from account limit 3, 5
Insert statement: insert into Table Name (Field List) values (Value List ). For example, insert into person (name, age) values ('chuanzhi ', 3)
Update statement: Update table name: Set field name = value: where Condition Clause. For example, update person set name = 'chuanzhi 'Where id = 10
Delete statement: delete from table name where Condition Clause. For example, delete from person where id = 10
Use sqlitedatabase to operate the SQL statement () method of SQLite database:
Sqlitedatabase DB = ....;
Db.exe csql ("insert into person (name, age) values ('chuanzhi pods', 4 )");
DB. Close ();
Rawquery () is used to execute the SELECT statement. The example is as follows: sqlitedatabase DB = ....;
Cursor cursor = dB. rawquery ("select * From person", null );
While (cursor. movetonext ()){
Int personid = cursor. getint (0); // obtain the value of the first column. The index of the first column starts from 0.
String name = cursor. getstring (1); // obtain the value of the second column
Int age = cursor. getint (2); // get the value of the third column
}
Cursor. Close ();
DB. Close ();
The first parameter of the rawquery () method is the SELECT statement. The second parameter is the value of the placeholder parameter in the SELECT statement. If the SELECT statement does not use a placeholder, this parameter can be set to null. An example of a SELECT statement with placeholder parameters is as follows:
Cursor cursor = dB. rawquery ("select * From person where name like? And age =? ", New string [] {" % Chuanzhi % "," 4 "});
CursorIs the result set cursor, used for Random Access to the result set. If you are familiar with JDBC, in factCursorAndResultset in JDBCFunctions are similar.
About cursor
When you understand and use Android cursor, you must first know several things about cursor:
- Cursor is a set of rows.
- Use movetofirst () to locate the first line.
- You must know the name of each column.
- You must know the Data Type of each column.
- Cursor is a random data source.
- All data is obtained by subscript.
Important cursor Methods: