vendredi 24 juillet 2015

Delete all but top n from database table in SQLite for java/android

I am new in java but I try coding:

      SQLiteDatabase db = this.getWritableDatabase();
        //db.execSQL("delete from " + TABLE_BOOKMARKS + " where " + KEY_ID + "  not in (select " + KEY_ID + " from " + TABLE_BOOKMARKS + " order by " + KEY_ID + " limit 10)" );
        //db.execSQL("DELETE FROM " + TABLE_BOOKMARKS + " WHERE " + KEY_ID + " NOT IN (SELECT TOP 10 " + KEY_ID + " FROM " + TABLE_BOOKMARKS + ")" );

        db.execSQL("Delete From "+TABLE_BOOKMARKS+"  where "+ KEY_ID  + " not in (Select Top 10 "+ KEY_ID +" from "+ TABLE_BOOKMARKS + " order by " + KEY_ID +")");
        db.close(); // Closing database connection

you can see in the code, I try 3 different code but when I run it my application return error and will be stop!

I got this error:

android.database.sqlite.SQLiteException: near "10": syntax error: , while compiling: Delete From bookmarks where id not in (Select Top 10 id from bookmarks order by id)

Aucun commentaire:

Enregistrer un commentaire