DROP TABLE如果EXISTS在SQLite android中不起作用

时间:2017-06-02 14:27:30

标签: android database sqlite android-sqlite

我试图在SQLiteOpenHelper的onUpgrade()方法中删除一个表。我得到一个奇怪的错误"没有这样的表"在查询中: DROP TABLE IF EXISTS certification_categories

代码是:

private static final String DROP_CERTIFICATION_CATEGORIES_TABLE = "DROP TABLE IF EXISTS certification_categories";
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
     db.execSQL(DROP_CERTIFICATION_CATEGORIES_TABLE);      
}

并且堆栈跟踪是:

android.database.sqlite.SQLiteException: no such table: main.Auction (code 1): , while compiling: DROP TABLE IF EXISTS certification_categories
                                                                            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
                                                                            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
                                                                            at com.db.DataBaseHelper.recreate(DataBaseHelper.java:135)
                                                                            at com.db.DataBaseHelper.onDowngrade(DataBaseHelper.java:125)
                                                                            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:254)
                                                                            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
                                                                            at com.dao.SyncProgressDao.getUnfinishedAuctionIds(SyncProgressDao.java:45)
                                                                            at com.service.SyncProgressService.getUnfinishedAuctionIds(SyncProgressService.java:69)
                                                                            at com.background.AuctionSync.syncUnfinished(AuctionSync.java:102)
                                                                            at com.background.AuctionSync.onHandleIntent(AuctionSync.java:97)
                                                                            at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:68)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.os.HandlerThread.run(HandlerThread.java:61)

2 个答案:

答案 0 :(得分:0)

在删除表之前打开数据库...

private static final String DROP_CERTIFICATION_CATEGORIES_TABLE = "DROP TABLE IF EXISTS certification_categories";
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
         db = openorCreateDatabase("DB_NAME",this.MODE_PRIVATE,null);
         db.execSQL(DROP_CERTIFICATION_CATEGORIES_TABLE);      
} 

答案 1 :(得分:0)

我弄清楚造成这种情况的原因。另一个表与certification_categories表有一个外键关联。

因此,首先删除该表然后删除certification_categories解决了这个问题。

然而,他们可能会标记出相应的错误,而不是“没有这样的表格&#39;使事情变得更容易。