lask
lask

Reputation: 13

How to delete one row in Android?

I have a problem when to deleting a row in ListView on android, I am using SQLite.

This is my class to delete a file (only need remove item the in database).

    public void deleteCallWhenUploadSuccess(String fileNameWhis)
    {
        db = callDatabaseHelper.getReadableDatabase();
        String where = CallDatabaseHelper.FILE_NAME + "=" + fileNameWhis;
        db.delete(CallDatabaseHelper.TABLE_NAME, where, null);
    }

And in class, I call to using this.

dao.deleteCallWhenUploadSuccess(filename);

But it throws exception:

e: "sqlite.SQLiteException: near "2016": syntax error (code 1):, while compiling:

DELETE FROM recordStatus WHERE fileName=109092 2016-03-17 01.018.03.mp3"

Seem it missing " mark near WHERE "fileName

I tried to add:

String where = CallDatabaseHelper.FILE_NAME + "=" + "'"'" + fileNameWhis;

But the error still exists. How to pass this error? And use DELETE statement to delete a file with fileName, in this case, it has many spaces and special characters in fileName?

Upvotes: 1

Views: 68

Answers (2)

AlbertMarkovski
AlbertMarkovski

Reputation: 85

Couple of things:

  • I would do a console log that spits out the file name at the top of the function, so you know its coming in formatted correctly
  • As pointed out in the other answer you also need to get a writeable database.
  • Use Where and WhereArgs in your query:

    db = callDatabaseHelper.getWriteableDatabase();
    String where = CallDatabaseHelper.FILE_NAME + " = ?";
    String [] whereArgs = new String[] {fileNameWhis}
    db.delete(CallDatabaseHelper.TABLE_NAME, where, whereArgs);
    

This is a safer way of doing queries, and may solve your issue.

Upvotes: 1

JpCrow
JpCrow

Reputation: 5077

You can delete this in an easier way

 db.delete(CallDatabaseHelper.TABLE_NAME, CallDatabaseHelper.FILE_NAME  + "=?", new String[]{fileNameWhis});

Upvotes: 0

Related Questions