-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
尝试运行DELETE MIN(产品名称)FROM产品;手动从SQLite管理器中查看它是否正确执行。我从来没有必须删除分钟,所以不是100%确定该查询是否正确。尝试这里提到的查询http://stackoverflow.com/questions/9819168/deleting-record-with-lowest-id – altoids
异常说明了这一切:'近“MIN”:语法错误'。如果你在SQLite网站上检查[DELETE查询的语法](https://www.sqlite.org/lang_delete.html),你可以看到你的查询是无效的(除此之外它可能在所有的SQL变种中都是无效的) 。 –
并且...请避免使用'+'WHERE 1“;'这不仅**完全无用**,而且**也可能有害**。 –