20

所以我的 DBAdapter 类中有一个问题,当我尝试打开数据库时它会崩溃:从 LogCat 我猜问题出在 onUpgrade 函数中:

 public void onUpgrade(SQLiteDatabase db, int oldVersion,
 int newVersion)
  {
       Log.w("SingleDBAdapter", "Upgrading database from version " + oldVersion
       + " to "
       + newVersion + ", which will destroy all old data");
       db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
       onCreate(db);
  }
 }

这是错误:

07-28 11:32:49.443: E/Database(1244): Failure 1 (near "122": syntax error) on 0x2435b0 when preparing 'DROP TABLE IF EXISTS 122'.
07-28 11:32:49.463: D/AndroidRuntime(1244): Shutting down VM
07-28 11:32:49.463: W/dalvikvm(1244): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-28 11:32:49.473: E/AndroidRuntime(1244): FATAL EXCEPTION: main
07-28 11:32:49.473: E/AndroidRuntime(1244): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shop.list/com.shop.list.main}: android.database.sqlite.SQLiteException: near "122": syntax error: **DROP TABLE IF EXISTS 122**
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.os.Looper.loop(Looper.java:123)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at java.lang.reflect.Method.invokeNative(Native Method)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at java.lang.reflect.Method.invoke(Method.java:521)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at dalvik.system.NativeStart.main(Native Method)
07-28 11:32:49.473: E/AndroidRuntime(1244): Caused by: android.database.sqlite.SQLiteException: near "122": syntax error: DROP TABLE IF EXISTS 122
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at com.shop.list.ListDBAdapter$DatabaseHelper.onUpgrade(ListDBAdapter.java:51)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:108)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at com.shop.list.ListDBAdapter.open(ListDBAdapter.java:60)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at com.shop.list.main.onCreate(main.java:60)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 11:32:49.473: E/AndroidRuntime(1244):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-28 11:32:49.473: E/AndroidRuntime(1244):     ... 11 more

我强调了这个问题,但我无法解决它:/

4

6 回答 6

39

问题似乎出在这句话上:

DROP TABLE IF EXISTS 122

其中122,如果它确实是要删除的表的名称,则没有分隔('例如,用 ),因此不能被视为名称。但是解析器期望那里有一个名字。只需将名称用单引号或双引号括起来,它应该可以工作:

db.execSQL("DROP TABLE IF EXISTS '" + DATABASE_TABLE + "'");
于 2012-07-28T11:53:12.760 回答
2

如果表名都是字母字符,那么您的原始代码就可以工作。由于表名不全是字母,因此需要用单引号括起来。

于 2014-03-08T19:55:31.257 回答
0

我认为你缺少一个';' 在 SQL 语句的末尾

除此之外,我刚刚在 sqlite3 控制台上进行了检查,似乎“122”是一个无效的表名:

sqlite> drop table if exists 122;
Error: near "122": syntax error
sqlite> drop table if exists test;
sqlite> 
于 2012-07-28T11:41:38.047 回答
0

db = 这是您的数据库处理程序类

db.execSQL("DROP TABLE IF EXISTS YourTableName");
于 2015-12-16T11:07:18.750 回答
-3

嗨,您应该使用调用语句:“context.deleteDatabase(DATABASE_NAME);”

在调用“onCreate();”之前

public void onUpgrade(SQLiteDatabase db, int oldVersion,
 int newVersion)
  {
       Log.w("SingleDBAdapter", "Upgrading database from version " + oldVersion
       + " to "
       + newVersion + ", which will destroy all old data");
       context.deleteDatabase(DATABASE_NAME);
       onCreate(db);
  }
 }
于 2014-04-29T05:34:43.943 回答
-3

你应该改变你的:

db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);

和:

db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE + ";");

此外,您需要检查 newVersion > oldVersion

于 2015-08-15T15:47:38.357 回答