我有一个高分的游戏,我试图删除最低分。除了我的应用程序在调用deleteProduct()方法时崩溃,一切正常。 Logcat说这是一个语法错误,但我真的不知道出了什么问题,因为这是我第一个使用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;
}
}
logcat debuging的主要部分
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 :(得分:2)
db.execSQL("DELETE FROM products WHERE productname = (SELECT MIN(productname) FROM products)");
而不是:
db.execSQL("DELETE MIN(" + COLUMN_PRODUCTNAME + ") FROM " + TABLE_PRODUCTS +";" );