SQLite's Affairs

Source: Internet
Author: User
Tags sqlite

Original address: http://www.cnblogs.com/wangmars/p/3914090.html

SQLite, is a lightweight database, is widely used in a lot of embedded products, because the use of very little resources, two of the operation of the way almost and we contact the database is not much, even only hundreds of k he will naturally be favored by the demand, Let's talk about how to read and write to him in such a lightweight database.

Before making a selection of contacts appear if a phone more than 2000 contacts, inserting into the database will be very time-consuming, different phone storage of different number of bars, the amount of storage and the memory of the phone has a great relationship, often depends on the phone memory, The following is the case of large amounts of data to write the SQLite batch query.

SqLite inserts data there are several

The first: Since the Inserthelper class has been deprecated on Android api17, it is based on a previously developed

Inserthelper ih = new Inserthelper (db, "table name");d b.begintransaction (); final int column 1= ih.getcolumnindex ("column 1"); final int column 2 = IH . Getcolumnindex ("Column 2"), try {for station s:buslines) {Ih.prepareforinsert (); ih.bind (column 1, corresponding value); ih.bind (column 2, corresponding value); Ih.execute ();} Db.settransactionsuccessful ();} finally {ih.close ();d b.endtransaction ();d b.close ();}

The second type:

Also in the sqlitedatabase.

public void Inertorupdatedatebatch (list<string> sqls) {sqlitedatabase db = Getwritabledatabase (); Db.begintransaction (); try {for (String sql:sqls) {   db.execsql (SQL);} The transaction flag is set to be successful, and transaction db.settransactionsuccessful () is committed when the transaction is ended; catch (Exception e) {e.printstacktrace ();} finally {//End transaction db.endtransaction ();d b.close ();}}

The third type: Sqlitedatabase db.insert ("table_name", NULL, contentvalues) can also be bulk inserted

public void InsertData (insert data) {db.begintransaction ();//Manually Set start transaction for (Contentvalues v:list) {db.insert ("table name", NULL, V);} Db.settransactionsuccessful (); Set transaction success, do not set automatically rollback does not commit db.endtransaction (); Processing complete db.close ()}

The fourth kind: sqlitestatement personal prefer this way, the data processing look very clear

String sql = "INSERT into table name (corresponding column) VALUES (?)"; Sqlitestatement stat = db.compilestatement (sql);d b.begintransaction (); for (dataset) {  //loop the data to be inserted} Db.settransactionsuccessful ();d b.endtransaction ();d b.close ();

Summary: The above several methods are used in the database of the transaction this thing, SQLite statement in which only walk once, the other is the data loop into the database objects, so than before with the object inserted, and then use for in the peripheral loop fast do not know how many times, before inserting more than 2000 data 300 milliseconds , it is also very fast for tens of thousands of data.

SQLite's Affairs

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.