简体   繁体   中英

delete a row using column field name - SQlite (SQLiteOpenHelper Android)

I need to delete a row in database (SQLite)

I am trying to delete a row using the file name. File is a field in the database column.

public void deleteRow (String file){
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete("checkList", "DELETE FROM checkList Where file = "+file, null);
}

What I get is:

Process: com.example.operacioneschecklist, PID: 29351 android.database.sqlite.SQLiteException: near "DELETE": syntax error (code 1 SQLITE_ERROR[1]): , while compiling: DELETE FROM checkList WHERE DELETE FROM checkList Where file = SE 062 Merlo20:05:23:12.txt

This is the class code That I implemented:

class MyDB extends SQLiteOpenHelper {

    private static final int VERSION_BASEDATOS = 1;

    private static final String NAME = "checkList.db";

    private static final String TABLA_CHECK_LIST = "CREATE TABLE IF NOT EXISTS checkList" +
            "(id INTEGER PRIMARY KEY AUTOINCREMENT, file TEXT, se TEXT, date TEXT, aux TEXT)";

    public MyDB(Context context) {
        super(context, NAME, null, VERSION_BASEDATOS);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLA_CHECK_LIST);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS checkList");
        onCreate(db);
    }

    public boolean deleteDB(Context context) {
        return context.deleteDatabase(NAME);
    }

    public void insert(String file, String se, String date, String aux) {
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            ContentValues valores = new ContentValues();
            valores.put("file", file);
            valores.put("se", se);
            valores.put("date", date);
            valores.put("aux", aux);
            db.insert("checkList", null, valores);
            db.close();
        }
    }

    public List<String> loadHandler() {
        List<String> list = new ArrayList<String>();
        String query = "Select*FROM checkList";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        while (cursor.moveToNext()) {
            int result_0 = cursor.getInt(0);
            String result_1 = cursor.getString(1);
            String result_2 = cursor.getString(2);
            String result_3 = cursor.getString(3);
            String result_4 = cursor.getString(4);

            list.add(result_1 + "%:&" + result_2 + "%:&" + result_3 + "%:&" + result_4);
        }
        cursor.close();
        db.close();
        return list;
    }

    public int getRowCount() {
        String query = "Select*FROM checkList";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        return cursor.getCount();
    }

    public void deleteFirstRow() {
        SQLiteDatabase db = this.getWritableDatabase();
        String where = "id < (select id from checkList order by id limit 15, 1)";
        db.delete("checkList", where, null);
    }

    public void orderIt() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("VACUUM");
    }

    public String getLastHash() {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "Select*FROM checkList";
        Cursor cursor = db.rawQuery(query, null);
        cursor.moveToLast();
        return cursor.getString(1);
    }

    public void deleteRow(String file) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete("checkList", "DELETE FROM checkList Where file = " + file, null);
    }
}

The problem is that in the 2nd argument of delete() you use the full sql statement, which is not nedeed and you concatenate the variable file to the sql statement's string without surrounding it with single quotes.
But even if you did, it is not the safe and recommended way.
Pass the parameter file in the 3d argument of the method delete() and use a ? placeholder inside the sql statement which will be replaced by the value of file , properly quoted, when the statement will be parsed:

db.delete("checkList", "file = ?", new String[] {file});

The 2nd argument of delete() is the WHERE clause without the keyword WHERE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM