我有一个关于在android中的SQLite数据库表中插入默认值的问题。我有一个DBHandler(显然)负责创建数据库,表和执行CRUD查询。
但我似乎无法弄清楚的是,如何在表格中插入默认值。 首先,我尝试使用以下方法在我的DBHandler中插入默认值:
// Write settings to db
public void addSetting(String key, String value) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(SETTING_KEY, key);
values.put(SETTING_VALUE, value);
db.insert(TABLE_APP_SETTINGS, null, values);
db.close();
}
使用onCreate()方法调用它:
@Override
public void onCreate(SQLiteDatabase db) {
// creating tables
db.execSQL(DATABASE_CREATE);
db.execSQL(CREATE_TABLE_SETTINGS);
// insert default values
this.addSetting("access_admin_fingers", "2");
}
但由于某种原因,这导致应用程序崩溃,给我以下LogCat消息:
02-18 09:43:52.050: W/dalvikvm(14297): threadid=1: thread exiting with uncaught exception (group=0x41f3d930)
02-18 09:43:52.060: E/AndroidRuntime(14297): FATAL EXCEPTION: main
02-18 09:43:52.060: E/AndroidRuntime(14297): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.customerratingsystem/com.customerratingsystem.AdminActivity}: java.lang.IllegalStateException: getDatabase called recursively
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.os.Looper.loop(Looper.java:137)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-18 09:43:52.060: E/AndroidRuntime(14297): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 09:43:52.060: E/AndroidRuntime(14297): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 09:43:52.060: E/AndroidRuntime(14297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
02-18 09:43:52.060: E/AndroidRuntime(14297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
02-18 09:43:52.060: E/AndroidRuntime(14297): at dalvik.system.NativeStart.main(Native Method)
02-18 09:43:52.060: E/AndroidRuntime(14297): Caused by: java.lang.IllegalStateException: getDatabase called recursively
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:204)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-18 09:43:52.060: E/AndroidRuntime(14297): at com.db.DBHandler.addSetting(DBHandler.java:69)
02-18 09:43:52.060: E/AndroidRuntime(14297): at com.db.DBHandler.onCreate(DBHandler.java:51)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-18 09:43:52.060: E/AndroidRuntime(14297): at com.db.DBHandler.getSetting(DBHandler.java:83)
02-18 09:43:52.060: E/AndroidRuntime(14297): at com.customerratingsystem.AdminActivity.onCreate(AdminActivity.java:28)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.app.Activity.performCreate(Activity.java:5104)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-18 09:43:52.060: E/AndroidRuntime(14297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
然后我尝试通过从我的UI调用addSetting方法来插入这个“默认值”,如下所示:
db = new DBHandler(getApplicationContext());
db.addSetting("access_admin_fingers", "2");
哪个有效!但我的问题是:为什么我无法在DBHandler中使用此方法?
另外,在Android SQLite数据库中插入默认值的探测器是什么?
答案 0 :(得分:6)
您已在SQLiteDatabase
中拥有onCreate()
个对象。您可以更改方法以获取SQLiteDatabase
参数:
public void addSetting(SQLiteDatabase db, String key, String value) {
ContentValues values = new ContentValues();
values.put(SETTING_KEY, key);
values.put(SETTING_VALUE, value);
db.insert(TABLE_APP_SETTINGS, null, values);
}
并称之为:
@Override
public void onCreate(SQLiteDatabase db) {
// creating tables
db.execSQL(DATABASE_CREATE);
db.execSQL(CREATE_TABLE_SETTINGS);
// insert default values
this.addSetting(db, "access_admin_fingers", "2");
}
答案 1 :(得分:1)
有很多方法。您可以通过调用DB
本身来访问DBHelper
课程{/ 1}},如下所示:
SQliteDatabase Object
此处,DB为// insert default values
db.addSetting("access_admin_fingers", "2");
。
或者您可以直接在SQliteDatabase Object
方法中运行插入查询。