SQLite Database Data Storage

Source: Internet
Author: User

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:

  • Close ()
    Close the cursor to release resources
  • Copystringtobuffer (INT columnindex, chararraybuffer buffer)
    Retrieve the text of the requested column in the buffer and store it
  • Getcolumncount ()
    Returns the total number of all columns.
  • Getcolumnindex (string columnname)
    Returns the name of the specified column. If no Column exists,-1 is returned.
  • Getcolumnindexorthrow (string columnname)
    The specified column name is returned from scratch. If the column name does not exist, an illegalargumentexception exception is thrown.
  • Getcolumnname (INT columnindex)
    Returns the column name from the given index.
  • Getcolumnnames ()
    Returns the column name of a string array.
  • Getcount ()
    Returns the number of rows in cursor.
  • Movetofirst ()
    Move the cursor to the first line
  • Movetolast ()
    Move the cursor to the last line
  • Movetonext ()
    Move the cursor to the next row
  • Movetoposition (INT position)
    Move the cursor to an absolute position
  • Movetoprevious ()
    Move the cursor to the previous line

    Http://www.cnblogs.com/terryblog/archive/2010/07/05/1771459.htmlmore information reference

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.