/** The connection method of SQLite is actually a single connection method. Even if multiple threads are used, the * getwritabledatabase () and getreadabledatabase () are all synchronized methods, but it is not a static method *, so it only synchronizes the same object and has no effect on different objects * So a single entry can be provided when SQLite is used, to prevent deadlocks caused by modifying databases with multiple objects * You can provide a static Instance Object + its get method. * The connection can always be hung, even if getwritabledatabase () is called multiple times () it does not matter with the/or getreadabledatabase () method, * because you are only getting an existing connection * The database does not need to be closed and exited Program The system automatically recycles * In fact, the main issue is the scope of the synchronized keyword * But using an object does not know whether it will affect program efficiency */package test. service; import android. content. context; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqliteopenhelper; public class dbhelper extends sqliteopenhelper {Private Static final string db_name = "BOC. DB "; Private Static final int database_version = 1;/* Private Static object, which provides a static instance of SQLite operation for the entire application, and ensure that it can only be obtained through the static method gethelper (context) below. * avoid bypassing the synchronization method to change it */Private Static dbhelper instance during use; // The deadlock problem is mainly solved here, is static to solve the deadlock problem/*** private constructor, can only be used by themselves, prevent bypassing the synchronization method to generate multiple instances, * @ Param context */private dbhelper (context) {super (context, db_name, null, database_version);}/*** provides a single portal for applications, ensure that the application uses the same object to operate on the database, and the synchronization method will not expire due to different objects * @ Param context * @ return instance */public static dbhelper gethelper (context) {If (instance = NULL) instance = new dbhelper (context); Return instance ;}@ overridepublic void oncreate (sqlitedatabase dB) {// gold name table db.exe csql ("create table if not exists goldname" + "(_ id integer primary key autoincrement, g_name varchar unique )"); // historical information table db.exe csql ("create table if not exists goldinfo" + "(_ id integer primary key autoincrement, gi_tid integer, gi_bidprice double, gi_offerprice double, "+" gi_inserttime time not null default current_time) "); // db.exe csql (" create table if not exists gold_lastinfo "+" (_ id integer primary key autoincrement, gl_name varchar, gl_bidprice double, gl_offerprice double) "); // currency name table db.exe csql (" create table if not exists currencies "+" (_ id integer primary key autoincrement, c_name varchar unique )"); // export csql ("create table if not exists exchangeinfo" + "(_ id integer primary key autoincrement, ei_tid integer, ei_buying double, ei_cashbuying double," + "ei_selling double, ei_cashselling double, ei_inserttime time not null default current_time) "); // ccsql (" create table if not exists exchange_lastinfo "+" (_ id integer primary key autoincrement, el_name varchar, el_buying double, el_cashbuying double, "+" el_selling double, el_cashselling double) ") ;}@ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion ){}}
Http://download.csdn.net/download/zhiaimm/4513636