简体   繁体   中英

Android SQLite specific table row deletion

I have some trouble removing specific users from my SQLite database on my Android device. I made a simple method to delete a table row where table.name equals first input and table.surname equals second input.

Here is my method:

void deleteUser(db_operations opt, String name, String surname) {
    SQLiteDatabase sdb = opt.getWritableDatabase();
    if(validate(name, surname) == true) {
        name = name.replaceAll("\\s+",""); surname = surname.replaceAll("\\s+","");
        try {
            String DELETE_USER = "DELETE FROM " + tb_users.tb_name + " WHERE " + tb_users.name + "='" + name + "' AND " + tb_users.surname + "='" + surname + "'";
            sdb.execSQL(DELETE_USER);
            sdb.close();
            System.out.println("Deletion SUCCESS!");
        } catch (SQLException e) {
            System.out.println("Deletion FAILED!");
        }
    }
}

If I execute a DELETE FROM myTableName statement, every user is removed from the table and from my ScrollView which is ok, but if I execute the above method to remove a specific user, output gives:

Deletion SUCCESS!

but my table still has the record. The record also remains in my ScrollView list (made with LinearLayouts). The list is built dynamically. I've already checked if the data is good or not before my SQLite execution starts and it looks ok. I can't figure out why my method doesn't work. Maybe I've missed something.

(Posted on behalf of the question author) .

I got it working. I found out that my validation method was returning false output all the time (I made a typo in one of the conditions), that's why my method never got a chance to execute. Thanks for a reminder to use logs. Was able to track my typo.

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