gpt4 book ai didi

java - 如何在 Android SQLite 中插入默认值?

转载 作者:行者123 更新时间:2023-12-01 07:27:34 25 4
gpt4 key购买 nike

我有一个关于在 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 数据库中插入默认值的探测器方式是什么?

最佳答案

您已在 onCreate() 中拥有 SQLiteDatabase 对象。您可以更改方法以采用 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");
}

关于java - 如何在 Android SQLite 中插入默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21848557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com