2017-03-19 189 views
-1

我有高分遊戲,我試圖刪除得分最低。除了我的應用程序崩潰時,我的deleteProduct()方法被調用時,一切正常。 Logcat說這是一個語法錯誤,但我真的不知道什麼是錯誤的,因爲這是我的第一個SQLite應用程序。無法獲取SQLite的刪除記錄

public class MyDBHandler extends SQLiteOpenHelper { 

private static final int DATABASE_VERSION = 4; 
private static final String DATABASE_NAME = "products.db"; 
public static final String TABLE_PRODUCTS = "products"; 
public static final String COLUMN_ID = "_id"; 
public static final String COLUMN_PRODUCTNAME = "productname"; 

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
    super(context, DATABASE_NAME, factory, DATABASE_VERSION); 
} 


@Override 
public void onCreate(SQLiteDatabase db) { 
    String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" + 
      COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      COLUMN_PRODUCTNAME + " TEXT " + 
      ");"; 
    db.execSQL(query); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS); 
    onCreate(db); 
} 

//add a new row to the database 
public void addProduct(Products product){ 

    SQLiteDatabase db = this.getReadableDatabase(); 
    if(DatabaseUtils.queryNumEntries(db, TABLE_PRODUCTS) <= 4){ 
     ContentValues values = new ContentValues(); 
     values.put(COLUMN_PRODUCTNAME, product.get_productname()); 
     db.insert(TABLE_PRODUCTS, null, values); 
     db.close(); 
    } 
    else if((!(DatabaseUtils.queryNumEntries(db, TABLE_PRODUCTS) <= 4)) /*&& new score is a highscore */){ 
     //how to insert new high score into correct spot and delete last high score 
     ContentValues values = new ContentValues(); 
     values.put(COLUMN_PRODUCTNAME, product.get_productname()); 
     db.insert(TABLE_PRODUCTS, null, values); 
     deleteProduct(); 
     db.close(); 

    } 
} 

//code coming from MainActivity 
public void addButtonClicked(String highscore1){ 
    Products product = new Products(highscore1); 
    addProduct(product); 
    updateDatabase(); 
} 

//code coming from MainActivity 
public void updateDatabase(){ 
     String dbString = databaseToString(); 
     MainActivity.productText.setText(dbString); 
} 

//delete a product from the database 
public void deleteProduct(){ 
    SQLiteDatabase db = getWritableDatabase(); 
    //db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";"); 
    db.execSQL("DELETE MIN(" + COLUMN_PRODUCTNAME + ") FROM " + TABLE_PRODUCTS +";"); 
} 


//print out the database as a string 
public String databaseToString(){ 
    String dbString = ""; 
    SQLiteDatabase db = getWritableDatabase(); 
    String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1"; 


    //Cursor point to location in your results 
    Cursor c = db.query(TABLE_PRODUCTS, null, null, null, null, null, COLUMN_PRODUCTNAME +" DESC"); 
    //Cursor c = db.rawQuery(query, null); 
    //Move to the first row in your results 
    c.moveToFirst(); 

    while(!c.isAfterLast()){ 
     if(c.getString(c.getColumnIndex("productname")) != null){ 
      dbString += c.getString(c.getColumnIndex("productname")); 
      dbString += "\n"; 
     } 
     c.moveToNext(); 

    } 


    db.close(); 
    return dbString; 

} 

}

調試運行

22688/com.example.emilythacker.myapplication E/SQLiteLog: (1) near "MIN": syntax error 
22688/com.example.emilythacker.myapplication E/AndroidRuntime: FATAL EXCEPTION: main 
                        Process: com.example.emilythacker.myapplication, PID: 22688 
                        android.database.sqlite.SQLiteException: near "MIN": syntax error (code 1): , while compiling: DELETE MIN(productname) FROM products; 
                        ################################################################# 
                        Error Code : 1 (SQLITE_ERROR) 
                        Caused By : SQL(query) error or missing database. 
                         (near "MIN": syntax error (code 1): , while compiling: DELETE MIN(productname) FROM products;) 
at com.example.emilythacker.myapplication.MyDBHandler.deleteProduct(MyDBHandler.java:76) 
                         at com.example.emilythacker.myapplication.MyDBHandler.addProduct(MyDBHandler.java:53) 
                         at com.example.emilythacker.myapplication.MyDBHandler.addButtonClicked(MyDBHandler.java:62) 
                         at com.example.emilythacker.myapplication.GameScreen.cancel(GameScreen.java:152) 
                         at com.example.emilythacker.myapplication.GameScreen.access$000(GameScreen.java:18) 
                         at com.example.emilythacker.myapplication.GameScreen$1.run(GameScreen.java:34 
+0

嘗試運行DELETE MIN(產品名稱)FROM產品;手動從SQLite管理器中查看它是否正確執行。我從來沒有必須刪除分鐘,所以不是100%確定該查詢是否正確。嘗試這裏提到的查詢http://stackoverflow.com/questions/9819168/deleting-record-with-lowest-id – altoids

+1

異常說明了這一切:'近「MIN」:語法錯誤'。如果你在SQLite網站上檢查[DELETE查詢的語法](https://www.sqlite.org/lang_delete.html),你可以看到你的查詢是無效的(除此之外它可能在所有的SQL變種中都是無效的) 。 –

+0

並且...請避免使用'+'WHERE 1「;'這不僅**完全無用**,而且**也可能有害**。 –

回答

2

logcat中的主要部分您查詢的結構是錯誤的。試試這個:

db.execSQL("DELETE FROM products WHERE productname = (SELECT MIN(productname) FROM products)"); 

代替:

db.execSQL("DELETE MIN(" + COLUMN_PRODUCTNAME + ") FROM " + TABLE_PRODUCTS +";");