gpt4 book ai didi

android - 在我的 DbHelper 类的 oCreate 中插入记录时递归调用 getDatabase

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:47 25 4
gpt4 key购买 nike

当我尝试在从 SQLiteOpenHelper 扩展的 DbHelper 类的 onCreate 方法中插入记录时,会出现此错误。
我发现了一些类似的主题,例如:
Android getDatabase called recursively
getWritableDatabase called recursively
getDatabase called recursively但这些对我没有帮助。

public class DbHelper extends SQLiteOpenHelper{

public int x=0;
public DbHelper(Context context) {
super(context, "shareholders.db", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
try {
sql = "CREATE TABLE IF NOT EXISTS settings (name text,value text)";
Log.d("Ehsan", sql);
db.execSQL(sql);


ContentValues cv = new ContentValues();
cv.put("name", "Username");
cv.put("value", "");
insert("settings", cv);

cv.put("name", "Password");
cv.put("value", "");
insert("settings", cv);

cv.put("name", "PersonId");
cv.put("value", "");
insert("settings", cv);
} catch (Exception e) {
xLog.error(e.getMessage());
}
}


public long insert(String table,ContentValues cv){
SQLiteDatabase mydb =this.getWritableDatabase();
return mydb.insert(table,null, cv);
}

}

这行代码要执行时会报错:

insert("settings", cv);

日志猫:

11-10 11:46:53.325: W/dalvikvm(19579): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
11-10 11:46:53.325: E/AndroidRuntime(19579): FATAL EXCEPTION: main
11-10 11:46:53.325: E/AndroidRuntime(19579): java.lang.IllegalStateException: Could not execute method of the activity
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.view.View$1.onClick(View.java:3599)
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.view.View.performClick(View.java:4204)
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.view.View$PerformClick.run(View.java:17355)
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.os.Handler.handleCallback(Handler.java:725)
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.os.Handler.dispatchMessage(Handler.java:92)
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.os.Looper.loop(Looper.java:137)
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-10 11:46:53.325: E/AndroidRuntime(19579): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 11:46:53.325: E/AndroidRuntime(19579): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 11:46:53.325: E/AndroidRuntime(19579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-10 11:46:53.325: E/AndroidRuntime(19579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-10 11:46:53.325: E/AndroidRuntime(19579): at dalvik.system.NativeStart.main(Native Method)
11-10 11:46:53.325: E/AndroidRuntime(19579): Caused by: java.lang.reflect.InvocationTargetException
11-10 11:46:53.325: E/AndroidRuntime(19579): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 11:46:53.325: E/AndroidRuntime(19579): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.view.View$1.onClick(View.java:3594)
11-10 11:46:53.325: E/AndroidRuntime(19579): ... 11 more
11-10 11:46:53.325: E/AndroidRuntime(19579): Caused by: java.lang.IllegalStateException: getDatabase called recursively
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:204)
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
11-10 11:46:53.325: E/AndroidRuntime(19579): at ClassLibrary.DbHelper.insert(DbHelper.java:76)
11-10 11:46:53.325: E/AndroidRuntime(19579): at ClassLibrary.DbHelper.onCreate(DbHelper.java:56)
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
11-10 11:46:53.325: E/AndroidRuntime(19579): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
11-10 11:46:53.325: E/AndroidRuntime(19579): at ClassLibrary.DbHelper.insert(DbHelper.java:76)
11-10 11:46:53.325: E/AndroidRuntime(19579): at com.example.shareholders.entities.Person.insert(Person.java:99)
11-10 11:46:53.325: E/AndroidRuntime(19579): at com.example.shareholders.Sync.doSync2(Sync.java:71)
11-10 11:46:53.325: E/AndroidRuntime(19579): at com.example.shareholders.AppMenu.clickHandler(AppMenu.java:56)
11-10 11:46:53.325: E/AndroidRuntime(19579): ... 14 more

我该怎么办?

最佳答案

您似乎有一个自定义的 insert() 方法。它可能调用 getWritableDatabase() 调用 getDatabase() 并且它会抛出这个异常,因为 onCreate() 已经在 getDatabase()。只需直接使用 db.insert(),其中 db 是传递给 onCreate()SQLiteDatabase 对象,无需调用 getWritableDatabase()。这与您链接的所有三个问题本质上是相同的问题和解决方案。

之前您的问题有另一个先前的问题,解决方案如下,包括上面的 insert() 修复:

您的表格没有您在 ContentValues 中指定的列名。

CREATE TABLE settings 中仅指定了 namevalue 列,但您正在尝试将数据插入 Username 列密码personId

记录完整的异常堆栈跟踪(而不仅仅是消息)以及在记录异常的任何地方读取异常也很有用。

如果我正确理解您的 Intent ,您需要像这样的三个名称-值对行:

ContentValues cv = new ContentValues();
cv.put("name", "Username");
cv.put("value", "");
db.insert("settings", cv);

cv.clear();
cv.put("name", "Password");
cv.put("value", "");
db.insert("settings", cv);

cv.clear();
cv.put("name", "personId");
cv.put("value", "");
db.insert("settings", cv);

或具有单行和给定列的表格:

sql = "CREATE TABLE IF NOT EXISTS settings (Username text,Password text,personId)";

关于android - 在我的 DbHelper 类的 oCreate 中插入记录时递归调用 getDatabase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19889103/

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