Android SQLite Data Storage

Source: Internet
Author: User

SQLite is a lightweight database. It is used on android. A database file represents a database. The SQL syntax is basically the same as that of mysql. The expressions are different.

Obtain the SQLiteDatabase object

SQLiteDatabase Method

Static SQLiteDatabaseOpenDatabase (String path, SQLiteDatabase. CursorFactory factory, int flags)

E: SQLiteDatabase db = SQLiteDatabase. openDatabase (path, factory, flags); // open the path file represented by path

Static SQLiteDatabaseOpenOrCreateDatabase (File file, SQLiteDatabase. CursorFactory factory)

E: SQLiteDatabase db = SQLiteDatabase. openOrCreateDatabase (mFile, factory); // open the database file corresponding to the mFile. If the file does not exist, create

Static SQLiteDatabaseOpenOrCreateDatabase (String path, SQLiteDatabase. CursorFactory factory );

E: SQLiteDatabase db = SQLiteDatabase. openOrCreateDatabaes (mPath, factory); // open the database file corresponding to mPath. If the file does not exist, create

========================================================== ========================================================== ======================================

SQLiteDatabase provides several methods to operate databases

ExecSQL (String SQL, Object [] objs); // Execute SQL statements with placeholders

ExexSQL (String SQL );// Directly execute the SQL statement

Insert (String table_name, String nullColumn, ContentValues values); // execute the SQL insert statement

Delete (String table_name, String where, String [] whereArgs); // delete

Update (String table_name, ContentValues values, String where, String [] whereArgs); // update

 

For example:

Name Age
Admin 20

 

Db.exe cSQL ("select * from table_name where id>? ", New int [] {10 });//? It indicates a placeholder. The placeholder must be of the same type and sequence as the following array.

Db.exe cSQL ("select * from table_name where id> 10") // similar to the first sentence, execute select * from table_name where id> 10 in a mysql statement;

It seems that only execSQL (xc) is used. This statement basically solves all the needs... add, delete, modify, and query...

 

 

ContentValues values = new ContentValues ();

Values. put ("name", "admin ");

Values. put ("age", "20"); // values. put (columnName, value) ;=> Map. put (key, value );

Db. insert (table_name, null, values); // insert records in the table_name Table, column name = value admin age = 20

// Equivalent to insert into table_name (name, age) values ("admin", 20 );

 

 

Db. delete ("table_name", "name like", new String [] {"a % "}); // delete a row whose name column value starts with a record ==> delete table_name where name like "a % ";

 

Values. put ("age", "25 ");

Db. update ("table_name", values, "age =", new String [] ("20"); // update the record whose age is 20, the modified value is placed in values ==> update table_name set age = 25 where age = 20;

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.