-1
Ich habe ein Spiel mit hohen Punktzahlen und versuche, die niedrigste Punktzahl zu löschen. Alles funktioniert, außer dass meine App abstürzt, wenn meine deleteProduct() -Methode aufgerufen wird. Logcat sagt, dass es ein Syntaxfehler ist, aber ich weiß wirklich nicht, was falsch ist, da dies meine erste Anwendung mit SQLite ist.Kann SQLite nicht einen Datensatz löschen
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;
}
}
Hauptteile 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
versuchen DELETE MIN läuft (Produktname) FROM Produkte; manuell aus dem SQLite-Manager, um zu sehen, ob es korrekt ausgeführt wird. Ich musste nie um min. Löschen, also nicht 100% sicher, ob diese Abfrage korrekt ist. Versuchen Sie die Abfrage, die hier erwähnt wird http://stackoverflow.com/questions/9819168/deleting-record-with-lowest-id – altoids
Die Ausnahme sagt alles: 'nahe" MIN ": Syntaxfehler'. Wenn Sie die [Syntax von DELETE-Abfragen] (https://www.sqlite.org/lang_delete.html) auf der SQLite-Website überprüfen, können Sie sehen, dass Ihre Abfrage ungültig ist (abgesehen davon, dass sie in wahrscheinlich allen Varianten von SQL ungültig ist). . –
Und ... Bitte vermeiden Sie '+" WHERE 1 ";' das ist nicht nur ** völlig nutzlos **, sondern auch ** potentiell schädlich **. –