0

我已经编写了在扩展类的类onCreate()方法中创建表的命令。打开数据库时,第一次调用该方法。但仍然有一个错误说 tablecorporate_boxes 已经存在。DatabaseManagerSQLiteOpenHelperonCreate()

打开方法

public DatabaseManager open() {  

   ourHelper = new DatabaseManager(context);  
   ourDatabase = ourHelper.getWritableDatabase();  
   return this;  

}

Oncreate 方法

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE_TABLE2);
}

DATABASE_CREATE_TABLE2是一个字符串,其定义如下,

private static final String DATABASE_CREATE_TABLE2 = "CREATE TABLE "
+ DATABASE_TABLE2 + " ( " + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_PRODUCT
+ " TEXT NOT NULL, " + KEY_PRICE + " TEXT NOT NULL," + KEY_IMAGEID
+ " TEXT NOT NULL );";

我也试过了,CREATE TABLE IF NOT EXISTS但它仍然给出了同样的错误。

这是正在显示的堆栈跟踪。

02-21 07:59:38.296: E/AndroidRuntime(1984): FATAL EXCEPTION: main
02-21 07:59:38.296: E/AndroidRuntime(1984): android.database.sqlite.SQLiteException: table corporate_boxes already exists (code 1): , while compiling: CREATE TABLE corporate_boxes ( _id INTEGER PRIMARY KEY AUTOINCREMENT, _name TEXT NOT NULL, _price TEXT NOT NULL,_imageid TEXT NOT NULL );
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at appistic.services.tricouschocolates.categories.DatabaseManager.onCreate(DatabaseManager.java:199)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at appistic.services.tricouschocolates.categories.DatabaseManager.open(DatabaseManager.java:116)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at appistic.services.tricouschocolates.Products.onItemSelected(Products.java:51)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.widget.AdapterView.access$200(AdapterView.java:49)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.os.Handler.handleCallback(Handler.java:725)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.os.Looper.loop(Looper.java:137)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at java.lang.reflect.Method.invoke(Method.java:511)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 07:59:38.296: E/AndroidRuntime(1984):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

很可能您的问题是;字符,因为其他代码看起来正确。此字符可能会导致神秘问题,因此请尝试将其从 DML 语句中删除,它应该可以工作。

我不明白为什么if not exists在查询中使用时也会引发此错误,因为onCreate()我仅在您第一次对 db 使用某些“操作”时才调用,然后 db 始终只打开用于读取或写入或两者兼而有之。

于 2013-02-21T08:44:33.807 回答
0

更改数据库版本号并尝试一次它可能对您完全使用。

于 2013-02-21T09:01:34.813 回答