sqlite“DROP TABLE IF EXISTS”android中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11700686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:32:08  来源:igfitidea点击:

error in sqlite "DROP TABLE IF EXISTS" android

androidsqldatabasesqlitedrop-table

提问by eli

so i have a problem in my DBAdapter class its just crushes when i try to open the database: from the LogCat i guess the problem is in the onUpgrade function:

所以我在我的 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);
  }
 }

here is the error:

这是错误:

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.access00(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

i highlighted the problem but i cant solve it :/

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

回答by Andriy M

The problem seems to be with this statement:

问题似乎出在这个语句上:

DROP TABLE IF EXISTS 122

where 122, if it is really the name of the table to drop, is not delimited (with ', for instance) and thus cannot be treated as a name. But the parser expects a name there. Just enclose the name in single or double quotes, and it should work:

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

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

回答by johngray1965

if the table name were all alpha characters, your original code would have worked. Since the table name isn't all alpha, it needs to be surrounded with single quotes.

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

回答by Hans Hohenfeld

I think your missing a ';' at the end of your SQL statement

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

Beyond that I just checked on an sqlite3 console, it seems, that "122" is an invalid table name:

除此之外,我刚刚检查了一个 sqlite3 控制台,看来“122”是一个无效的表名:

sqlite> drop table if exists 122;
Error: near "122": syntax error
sqlite> drop table if exists test;
sqlite> 

回答by Sathish Gadde

db = This is your database handler class

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

db.execSQL("DROP TABLE IF EXISTS YourTableName");

回答by Manmohan Soni

Hi Think u should use call the statement : "context.deleteDatabase(DATABASE_NAME);"

嗨认为你应该使用调用语句:“context.deleteDatabase(DATABASE_NAME);”

before the call of "onCreate();"

在“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);
  }
 }

回答by VCHe

You should change your:

你应该改变你的:

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

with:

和:

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

Also, you need to check if newVersion > oldVersion

另外,您需要检查是否 newVersion > oldVersion