从SQLite中的数据库表中删除除了顶部n之外的所有内容,用于java / android

时间:2015-07-24 20:41:02

标签: java android sql sqlite

我是java的新手,但我正在尝试编码:

      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

你可以在代码中看到,我尝试了3种不同的方式但是当我运行它时我的应用程序返回错误并停止!

我收到了这个错误:

  

android.database.sqlite.SQLiteException:near“10”:语法错误:,   编译时:删除ID不在的书签(选择前10名)   id由书签订购的id)

1 个答案:

答案 0 :(得分:1)

你可以尝试

db.execSQL("Delete From "+TABLE_BOOKMARKS+"  where "+ KEY_ID  + " not in (Select "+ KEY_ID +" from "+ TABLE_BOOKMARKS + " order by " + KEY_ID +" limit 10)");