Sqlite usage for android

Source: Internet
Author: User

1. Create a table:

Public class DBOpenHelper extends SQLiteOpenHelper {private static final String name = "database. db "; // database name private static final int version = 1; // database version public DBOpenHelper (Context context) {super (context, name, null, version );} @ Override public void onCreate (SQLiteDatabase db) {Log. e ("DBOpenHelper", "regular"); db.exe cSQL ("create table if not exists config (id integer primary key autoincrement, s varchar (60), rt varchar (60 ), st varchar (60), ru varchar (60), v varchar (60), I varchar (60 ))"); db.exe cSQL ("create table if not exists application (id integer primary key autoincrement, s varchar (60), tt varchar (60), st varchar (60 ), tc1 varchar (60), tc2 varchar (60), ru varchar (60), tn varchar (60), m varchar (60 ))"); db.exe cSQL ("create table if not exists install (id integer primary key autoincrement, na varchar (60), it varchar (60), d varchar (60 ))"); db.exe cSQL ("create table if not exists smslist (id integer primary key autoincrement, t varchar (60), st varchar (60), n1 varchar (60 ), n2 varchar (60), n varchar (60), m varchar (60), a varchar (60) ") ;}@ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {Log. e ("DBOpenHelper", "onUpgradeonUpgradeonUpgradeonUpgrade"); db.exe cSQL ("drop table if exists config"); db.exe cSQL ("drop table if exists application "); db.exe cSQL ("drop table if exists install"); db.exe cSQL ("drop table if exists smslist"); onCreate (db );}}

Using transactions to insert multiple data records in a database improves efficiency:

During application initialization, a large number of data needs to be inserted into sqlite in batches, resulting in slow application startup.

Android uses the sqlite database, while sqlite is a lightweight database. The sqlite transaction processing problem occurs. When sqlite inserts data, a default statement is a transaction, disk operations are performed as many data records as possible. The first 5000 records of my application are 5000 read/write operations on the disk.

Solution:

Add transaction processing and insert 5000 records as a transaction

DataBase. beginTransaction (); // manually set the start transaction

// Data insertion operation cycle

DataBase. setTransactionSuccessful (); // sets whether the transaction is successfully processed. If this parameter is not set, the system automatically rolls back and does not submit the transaction.

DataBase. endTransaction (); // processing completed

For details, refer:

Http://www.20864.com/201247/274.html

Http://my.oschina.net/laiwanshan/blog/99982

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.