2015-10-18 64 views
0

我創建了一個名爲DRINK的表。我最初插入了兩行。升級時,我想刪除兩行並將我的主鍵重置爲1.我得到的行要刪除,但沒有任何運氣重置主鍵。有人知道如何做到這一點的語法,或者甚至有可能嗎?這是從我的onUpdate函數調用的函數:在Android中重置INTEGER PRIMARY KEY AUTOINCREMENT

private void updateMyDatabase(SQLiteDatabase db, int olderVersion, int newVersion){ 
    Log.v("DatabaseHelper","UPDATE MY DATABASE"); 
    if (olderVersion<1){ 
     db.execSQL("CREATE TABLE DRINK (_id INTEGER PRIMARY KEY AUTOINCREMENT," 
       + "NAME TEXT, " 
       + "DESCRIPTION TEXT, " 
       + "IMAGE_RESOURCE_ID INTEGER);"); 
     insertDrink(db,"latte","Espresso and Steamed Milk",R.drawable.latte); 
     insertDrink(db, "cappuccino", "This a Cappuccino", R.drawable.cap); 
    } 
    if (olderVersion < 2){ 
     db.execSQL("delete from DRINK"); 
     db.execSQL("alter table AUTO_INCREMENT =1"); //*** NEED HELP HERE*** 
     insertDrink(db, "coff", "Espresso and Steamed Milk", R.drawable.latte); 
    } 
} 

我在網上找到這段代碼,但它不工作。謝謝。

db.execSQL("alter table AUTO_INCREMENT =1"); 

回答

2

刪除行將不會重置ROWID。

SQLite的文檔指出:

SQLite keeps track of the largest ROWID that a table has ever held using the special SQLITE_SEQUENCE table. The SQLITE_SEQUENCE table is created and initialized automatically whenever a normal table that contains an AUTOINCREMENT column is created. The content of the SQLITE_SEQUENCE table can be modified using ordinary UPDATE, INSERT, and DELETE statements. But making modifications to this table will likely perturb the AUTOINCREMENT key generation algorithm.

這個SQL應該做你需要的東西:

UPDATE SQLITE_SEQUENCE SET seq = <n> WHERE name = <table> 

其中n是你想設置和表是你的表名

+0

我得到的序列此日誌錯誤:E/SQLiteLog:(1)在「<」附近:語法錯誤 –

+0

您是否將更改爲所需的值? –

+0

是的,我做到了。下面是確切的代碼行:db.execSQL(「UPDATE SQLITE_SEQUENCE SET seq = <1> WHERE name ='DRINK'」); –

相關問題