我正在使用单个数据库创建一个针对 API 17 的应用程序,完成后将有 300-700 行之间的文本非常重。我最初选择以编程方式创建我的数据库,将要添加到数据库的文本保存为 res/values 字符串文件中的 xml 文件中的字符串。
我已经阅读了有关使用我知道已弃用的 DatabaseUtils.InsertHelper() 和 SQLite 语句的对话,但是我不明白如何更改当前代码以提高填充数据库时的性能。
我与数据库相关的主要活动代码如下:
public class MainActivity extends FragmentActivity
implements //implementations for various fragments
public LawDbAdapter dbHelper;
LawDbAdapter db;
public SimpleCursorAdapter cursorAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
//code for creating the fragment and inflating the view
openDatabase();
}
private void openDatabase() {
db = new LawDbAdapter(this);
db.open();
//Clean all data
db.deleteAllEntries();
//Add some data
db.insertSomeEntrys();
}
private void showInfoNamedCursor(String namedCursor) {
Cursor cursor = db.fetchEntriesByName(namedCursor);
String a;
String b;
String c;
String d;
String e;
String f;
a = cursor.getString(cursor.getColumnIndexOrThrow("aaa"));
b = cursor.getString(cursor.getColumnIndexOrThrow("bbb"));
c = cursor.getString(cursor.getColumnIndexOrThrow("ccc"));
d = cursor.getString(cursor.getColumnIndexOrThrow("ddd"));
e = cursor.getString(cursor.getColumnIndexOrThrow("eee"));
f = cursor.getString(cursor.getColumnIndexOrThrow("fff"));
onLawSelected(0, name, section, act, definition, arrest, detain);
}
我的数据库适配器代码如下:
public class LawDbAdapter {
//static variables for the ROW NAMES of the Law table
//TAG for Log prints
private static final String TAG = "LawDbAdapter";
//db and db helpers
private static DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
//Database and table name/version
private static final String DATABASE_NAME = "Legislation";
private static final String SQLITE_TABLE = "Law";
private static final int DATABASE_VERSION = 1;
//reference to the current context
private final Context mCtx;
//String to create the database if it does not already exist
private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement," +
KEY_SECTION + "," +
KEY_NAME + "," +
KEY_DEFINITION + "," +
KEY_ACT + "," +
KEY_ARREST + ","+
KEY_KEYWORDS + ","+
KEY_GROUP + "," +
KEY_DETAINABLE +
");"
;
private static class DatabaseHelper extends SQLiteOpenHelper {
public static DatabaseHelper getInstance(Context ctx) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
if (mDbHelper == null) {
mDbHelper = new DatabaseHelper(ctx.getApplicationContext());
Log.d("DBADAPTER", "NEW DATABASE HELPER OBJECT CREATED");
}
return mDbHelper;
}
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
//create database
db.execSQL(DATABASE_CREATE);
System.out.println("onCreate() DATABASE CREATED " + db.toString());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
//drop old table
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
//call on create ot create new table
onCreate(db);
}
}
public LawDbAdapter(Context ctx) {
//reference current context
this.mCtx = ctx;
}
public LawDbAdapter open() throws SQLException {
DatabaseHelper.getInstance(mCtx);
mDb = mDbHelper.getWritableDatabase();
System.out.println("LawDb Adapter opne() DATABASE ADAPTER " + this.toString());
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
/**
* creates a new Law in the Law Table and puts in the Strings passed into a new ROW
* @param section
* @param name
* @param definition
* @param act
* @param arrest
* @param keywords
* @param group
* @return
*/
public long createEntry(String section, String name,
String definition, String act, String arrest, String keywords, String group, String detention) {
System.out.println("createEntry()");
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_SECTION, section);
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_DEFINITION, definition);
initialValues.put(KEY_ACT, act);
initialValues.put(KEY_ARREST, arrest);
initialValues.put(KEY_KEYWORDS, keywords);
initialValues.put(KEY_GROUP, group);
initialValues.put(KEY_DETAINABLE, detention);
return mDb.insert(SQLITE_TABLE, null, initialValues);
}
//delete all laws in the table
public boolean deleteAllEntries() {
int doneDelete = 0;
doneDelete = mDb.delete(SQLITE_TABLE, null , null);
Log.w(TAG, Integer.toString(doneDelete)+ " deleteAllLaws()");
return doneDelete > 0;
}
/*
* returns a whole row of SQLITE_TABLE in alphabetical order. if inputtext is null or 0 length then no selection is performed.
* IF a string has been passed then any ROWs that NAME contains the input text will be displayed, still in alphabetical order.
*/
public Cursor fetchEntriesByName(String inputText) throws SQLException {
Log.d(TAG, inputText + " fetchLawsByName()");
Cursor mCursor = null;
//will not filter if the parameter is null or of no length
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_SECTION, KEY_NAME, KEY_DEFINITION, KEY_ACT, KEY_ARREST, KEY_KEYWORDS, KEY_GROUP, KEY_DETAINABLE},
null, null, null, null, KEY_NAME + " ASC");
}
else {
//will filter the items showing only those that contain the filter parameter within the name ie dru in drugs.
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_SECTION, KEY_NAME, KEY_DEFINITION, KEY_ACT, KEY_ARREST, KEY_KEYWORDS, KEY_GROUP, KEY_DETAINABLE},
KEY_NAME + " like '%" + inputText + "%'", null,
null, null, KEY_NAME + " ASC", null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//returns laws based on a filter by keywords
public Cursor fetchEntriesByKeyword(String inputText) throws SQLException {
Log.w(TAG, inputText + " fetchLawsByKeyword()");
Cursor mCursor = null;
//will not filter if the parameter is null or of no length
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_SECTION, KEY_NAME, KEY_DEFINITION, KEY_ACT, KEY_ARREST, KEY_KEYWORDS, KEY_GROUP, KEY_DETAINABLE},
null, null, null, null, KEY_NAME + " ASC");
}
else {
//will filter the items showing only those that contain the filter parameter within the name ie dru in drugs.
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_SECTION, KEY_NAME, KEY_DEFINITION, KEY_ACT, KEY_ARREST, KEY_KEYWORDS, KEY_GROUP, KEY_DETAINABLE},
KEY_KEYWORDS + " like '%" + inputText + "%'", null,
null, null, KEY_NAME + " ASC", null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//will fetch all entries in alphabetical order
public Cursor fetchAllEntries() {
Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_SECTION, KEY_NAME, KEY_DEFINITION, KEY_ACT, KEY_ARREST, KEY_KEYWORDS, KEY_GROUP, KEY_DETAINABLE},
null, null, null, null, KEY_NAME + " ASC");
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/*
* returns a whole row of SQLITE_TABLE in alphabetical order. if inputtext is null or 0 length then no selection is performed.
* IF a string has been passed then any ROWs that ACT contains the input text will be displayed, still in alphabetical order.
*/
public Cursor fetchEntriesByAct(String inputText){
Log.w(TAG, inputText + " fetchLawsByAct()");
Cursor mCursor = null;
//will not filter if the parameter is null or of no length
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_SECTION, KEY_NAME, KEY_DEFINITION, KEY_ACT, KEY_ARREST, KEY_KEYWORDS, KEY_GROUP, KEY_DETAINABLE},
null, null, null, null, KEY_ACT);
}
else {
//will filter the items showing only those that contain the filter parameter within the name ie dru in drugs.
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_SECTION, KEY_NAME, KEY_DEFINITION, KEY_ACT, KEY_ARREST, KEY_KEYWORDS, KEY_GROUP, KEY_DETAINABLE},
KEY_ACT + " like '%" + inputText + "%'", null,
null, null, KEY_ACT, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/*
* returns a whole row of SQLITE_TABLE in alphabetical order. if inputtext is null or 0 length all rows in the passed GROUP
* are displayed. IF a string has been passed then any ROWs that NAME contains the input text will be displayed, still in alphabetical order.
* and in the originally passed group
*/
public Cursor fetchEntriesByGroup(String group, String inputText) throws SQLException {
Log.w(TAG, inputText + " fetchLawsByGroup()");
Cursor mCursor = null;
//will not filter by search if the parameter is null or of no length
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_SECTION, KEY_NAME, KEY_DEFINITION, KEY_ACT, KEY_ARREST, KEY_KEYWORDS, KEY_GROUP, KEY_DETAINABLE},
KEY_GROUP + " like '%" + group + "%'", null, null, null, KEY_NAME + " ASC");
//where KEY_GROUP = group
}
else {
//will filter the items showing only those that contain the filter parameter within the name ie dru in drugs.
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_SECTION, KEY_NAME, KEY_DEFINITION, KEY_ACT, KEY_ARREST, KEY_KEYWORDS, KEY_GROUP, KEY_DETAINABLE},
KEY_NAME + " like '%" + inputText + "%' AND "+ KEY_GROUP + " like '%" + group + "%'", null,
null, null, KEY_NAME + " ASC", null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//untested deleteMethod
public void deleteNamedEntry(String entryName) {
mDb.delete(DATABASE_NAME, KEY_NAME + "= ?" + entryName, null);
}
//this method creates all the laws in the table, it does so by referring to strings in R.java. Each line is is one law.
public void insertSomeEntrys() {
System.out.println("insertSomeEntries()");
//COPY PASTE SAMPLES
// MNEMONIC createEntry(null,MNEMONICNAME,DEFINITION,TOPIC,null,TAGS,GROUP, null);
// LAW createEntry(section,name,definition,act,arrest,tags,group, detention);
// createLaw(null, name, definition, null,null,tags,group,null);
// mCtx.getString(R.string
// DRUG createEntry(null,NAME,DEFINITION,NICKNAMES,null,TAGS,GROUP, null);
//SIGNS OF INTOXICATION
createEntry(null,mCtx.getString(R.string.signs_of_intoxication_name),mCtx.getString(R.string.signs_of_intoxication_definition),null,null,mCtx.getString(R.string.test_tag),null, null);
//PROCEDURAL ENTRIES (MAY BELONG TO SEVERAL SECTIONS)
//full detention; procedures/basic/law
createEntry(mCtx.getString(R.string.detention_section),mCtx.getString(R.string.detention_name),mCtx.getString(R.string.full_detention_definition),mCtx.getString(R.string.criminal_procedure_scotland_act),null,mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_procedures) + " " + mCtx.getString(R.string.group_basic)+ " " + mCtx.getString(R.string.group_law), null);
//human rights; procedures/basic/law
createEntry(mCtx.getString(R.string.human_rights_section),mCtx.getString(R.string.human_rights_name),mCtx.getString(R.string.human_rights_definition),mCtx.getString(R.string.human_rights_act),mCtx.getString(R.string.human_rights_arrest),mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_procedures)+ " " + mCtx.getString(R.string.group_basic)+ " " + mCtx.getString(R.string.group_law), null);
//S13 requirement; procedures/basic/law
createEntry(mCtx.getString(R.string.S13_section),mCtx.getString(R.string.S13_name),mCtx.getString(R.string.S13_definition),mCtx.getString(R.string.criminal_procedure_scotland_act),mCtx.getString(R.string.S13_arrest),mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_procedures)+ " " + mCtx.getString(R.string.group_basic)+ " " + mCtx.getString(R.string.group_law), mCtx.getString(R.string.detain_no));
//full arrest without warrant commonlaw and statute ; prcedures/basic/law
createEntry(null, mCtx.getString(R.string.full_arrest_without_warrant_name), mCtx.getString(R.string.full_arrest_without_warrant_definition), mCtx.getString(R.string.full_arrest_without_warrant_act), null, mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_procedures)+ " " + mCtx.getString(R.string.group_basic)+ " " + mCtx.getString(R.string.group_law), null);
//S18 CP(S)A; procedures/law
createEntry(mCtx.getString(R.string.S18_section),mCtx.getString(R.string.S18_name),mCtx.getString(R.string.S18_definition),mCtx.getString(R.string.criminal_procedure_scotland_act),null,mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_procedures)+ " " + mCtx.getString(R.string.group_law), null);
//entry to premises common law; procedures;basic/law
createEntry(null , mCtx.getString(R.string.entry_to_premises_common_law_name),mCtx.getString(R.string.entry_to_premises_common_law_definition),mCtx.getString(R.string.common_law),null, mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_procedures)+ " " + mCtx.getString(R.string.group_basic)+ " " + mCtx.getString(R.string.group_law), null);
//breach of bail; procedures;basic/law
createEntry(mCtx.getString(R.string.breach_of_bail_section),mCtx.getString(R.string.breach_of_bail_name),mCtx.getString(R.string.breach_of_bail_definition),mCtx.getString(R.string.criminal_procedure_scotland_act),mCtx.getString(R.string.breach_of_bail_arrest),mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_procedures)+ " " + mCtx.getString(R.string.group_basic)+ " " + mCtx.getString(R.string.group_law), mCtx.getString(R.string.detain_yes));
//cross border powers; procedures/law
createEntry(mCtx.getString(R.string.cross_border_arrest_section),mCtx.getString(R.string.cross_border_arrest_name),mCtx.getString(R.string.cross_border_arrest_definition),mCtx.getString(R.string.criminal_justice_and_public_order_act),null,mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_procedures)+ " " + mCtx.getString(R.string.group_law), null);
//CRIMES
//COMMON LAW
//Theft; crime/law/basic
createEntry(null,mCtx.getString(R.string.theft_name),mCtx.getString(R.string.theft_definition),mCtx.getString(R.string.common_law),mCtx.getString(R.string.common_law_arrest),mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_crime) + " " + mCtx.getString(R.string.group_basic) + " " + mCtx.getString(R.string.group_law), mCtx.getString(R.string.detain_yes));
//Theft HB; crime/law
createEntry(null,mCtx.getString(R.string.theft_HB_name),mCtx.getString(R.string.theft_HB_definition),mCtx.getString(R.string.common_law),mCtx.getString(R.string.common_law_arrest),mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_law) + " " + mCtx.getString(R.string.group_crime), mCtx.getString(R.string.detain_yes));
//Att Theft HBWI; crime;law
createEntry(null,mCtx.getString(R.string.Att_Theft_HBWI_name),mCtx.getString(R.string.Att_Theft_HBWI_definition),mCtx.getString(R.string.common_law),mCtx.getString(R.string.common_law_arrest),mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_law) + " " + mCtx.getString(R.string.group_crime), mCtx.getString(R.string.detain_yes));
//HBWI; crime/law
createEntry(null,mCtx.getString(R.string.HBWI_name),mCtx.getString(R.string.HBWI_definition),mCtx.getString(R.string.common_law),mCtx.getString(R.string.common_law_arrest),mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_law) + " " + mCtx.getString(R.string.group_crime), mCtx.getString(R.string.detain_yes));
//MISUSE OF DRUGS ACT
//drugs(possession); crime /law
createEntry("5(2) ","Possession of a Controlled Drug","Drugs are Bad M-Kay",mCtx.getString(R.string.misuse_of_drugs_act),"a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_crime)+ " " + mCtx.getString(R.string.group_law), mCtx.getString(R.string.detain_yes));
//drugs(search); search/law
createEntry("23 ","Search Power - Drugs","Police can search you for drugs, deal with it",mCtx.getString(R.string.misuse_of_drugs_act),"a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_search)+ " " + mCtx.getString(R.string.group_law), null);
//ROAD TRAFFIC ACT
//drink driving; traffic/law
createEntry("5 ","Drink Driving","Driving Drunk is a crime",mCtx.getString(R.string.road_traffic_act),"a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_traffic)+ " " + mCtx.getString(R.string.group_law), null);
//Take MV without authority; traffic/law
createEntry(mCtx.getString(R.string.s178_motor_vehicle_section),mCtx.getString(R.string.s178_motor_vehicle_name),mCtx.getString(R.string.s178_motor_vehicle_definition),mCtx.getString(R.string.road_traffic_act),mCtx.getString(R.string.s178_motor_vehicle_arrest),mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_law) + " " + mCtx.getString(R.string.group_traffic), mCtx.getString(R.string.detain_yes));
//CRIMINAL LAW CONSOLIDATION SCOTLAND ACT
//vandalism; crime/law/civilorder
createEntry(mCtx.getString(R.string.law_vandalism_section),mCtx.getString(R.string.law_vandalism_name),mCtx.getString(R.string.law_vandalism_definition),mCtx.getString(R.string.criminal_law_consolidation_scotland_act),mCtx.getString(R.string.law_vandalism_arrest)+ " "+ mCtx.getString(R.string.common_law_arrest),mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_crime) + " " + mCtx.getString(R.string.group_civil_order)+ " " + mCtx.getString(R.string.group_law), mCtx.getString(R.string.detain_yes));
//TEMP CRIME
createEntry("Section 1 ","Sexual Crime","definition of a sexual crime","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_sexual)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 4 ","Crime Crime","definition of a crime crime","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_crime)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 5 ","People Crime","definition of a people crime","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_people)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 6 ","Civil Crime","definition of a civil crime","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_civil_order)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 7 ","Licensing Crime","definition of a licensing crime","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_licensing)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 8 ","Terrorism Crime","definition","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_terrorism)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 9 ","Animal Crime","definition","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_animals)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 0 ","Crime","definition","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_firearms)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 12 ","Crime","definition","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_terrorism)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 11 ","Crime","definition","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_animals)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 13 ","Crime","definition","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_crime)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 14 ","Crime","definition","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_crime)+ " " + mCtx.getString(R.string.group_law), null);
createEntry("Section 15 ","Crime","definition","act","a",mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_crime)+ " " + mCtx.getString(R.string.group_law), null);
//MNEMONICS
createEntry(null,mCtx.getString(R.string.mnemonic_DRABC_name),mCtx.getString(R.string.mnemonic_DRABC_definition),mCtx.getString(R.string.mnemonic_DRABC_topic),null,mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_mnemonic), null);
//DRUGS
createEntry(null,mCtx.getString(R.string.drug_cannabis_name),mCtx.getString(R.string.drug_cannabis_description),mCtx.getString(R.string.drug_cannabis_nicknames),null,mCtx.getString(R.string.test_tag),mCtx.getString(R.string.group_drug), null); }
}