getDatabase递归调用时oCreate插入我的DbHelper类的记录 [英] getDatabase called recursively when insert a record in oCreate of my DbHelper class
问题描述
当我试图插入其中从SQLiteOpenHelper延长我的DbHelper类的onCreate方法的记录,这个错误会升高。结果
我找到了一些类似的主题这样的:结果
的Android getDatabase递归调用结果
<一href=\"http://stackoverflow.com/questions/9152907/getwritabledatabase-called-recursively\">getWritableDatabase递归调用结果
getDatabase递归调用的
但这些都不会帮我。
When I'm trying to insert a record in onCreate method of my DbHelper class which is extended from SQLiteOpenHelper, this error will be rised.
I've found some similar topics like these:
Android getDatabase called recursively
getWritableDatabase called recursively
getDatabase called recursively
but these are not going to help me.
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);
}
}
在这一行code的要执行的错误将提高:
the error will be raised when this line of code wants to execute:
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
我应该怎么办?
推荐答案
您似乎有一个自定义的插入()
方法。它可能调用 getWritableDatabase()
这就要求 getDatabase()
键,它会抛出,因为此异常的onCreate()
已经叫的背景下getDatabase()
。只需使用 db.insert()
直接其中分贝
是 SQLiteDatabase
传递给你的对象的onCreate()
,而不调用 getWritableDatabase()
。这基本上是相同的问题和解决方案,为您链接所有的三个问题。
You seem to have a custom insert()
method. It likely calls getWritableDatabase()
which calls getDatabase()
and it will throw this exception since onCreate()
is already called in the context of getDatabase()
. Just use db.insert()
directly where db
is the SQLiteDatabase
object passed to your onCreate()
, without calling getWritableDatabase()
. This is essentially the same issue and solution as in all the three questions you linked.
previously你的问题还有另外一个,之前问题是下面的解决方案,以插入()
从上面固定有:
Previously your question had another, prior problem the solution to which is below, with the insert()
fix from above included:
您的表没有你 ContentValues
指定列名。
Your table does not have the column names you specify in ContentValues
.
有只有名称
和在指定的值
列 CREATE TABLE设置
,但你试图将数据插入列用户名
,密码
和 PERSONID
。
There's only name
and value
columns specified in the CREATE TABLE settings
but you're trying to insert data into columns Username
, Password
and personId
.
这也是非常有用的日志已满的异常堆栈跟踪(而不仅仅是消息),何地登录他们阅读的异常。
It's also useful to log full exception stack traces (and not just the message), and to read the exceptions wherever you log them.
如果我理解正确你的意图,你想要么三个名称 - 值对行是这样的:
If I understand your intent correctly, you want either three name-value pair rows like this:
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);
或具有单列和给定列的表
or a table with single row and the given columns:
sql = "CREATE TABLE IF NOT EXISTS settings (Username text,Password text,personId)";
这篇关于getDatabase递归调用时oCreate插入我的DbHelper类的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!