Binoy babu
Binoy babu

Reputation: 1

SQLite DELETE not working

I'm using the following code to remove certain rows from my SQLite table.

                if (!cfile.exists()) {// remove invalid db files
                    database.rawQuery("DELETE FROM "
                            + DataBaseHelper.VFS_DATABASE_TABLE + " WHERE "
                            + DataBaseHelper.VIRTUAL_SYSTEM_COLUMN_PATH // TODO
                                                // not
                                                // removing
                                                // properly
                            + " IS ?", new String[] { pathcursora.getString(0) });
                    System.out.println("DELETE FROM "
                            + DataBaseHelper.VFS_DATABASE_TABLE + " WHERE "
                            + DataBaseHelper.VIRTUAL_SYSTEM_COLUMN_PATH
                            + " IS "+pathcursora.getString(0));
                }

But the rows are not getting deleted. Stack trace below:

02-28 23:13:27.564: D/NameManager.java(7098): Checking for whatever
02-28 23:13:27.587: I/System.out(7098): 1 : /mnt/sdcard/fsimages87
02-28 23:13:27.587: I/System.out(7098): 2 : /mnt/sdcard/fsimages3
02-28 23:13:27.595: I/System.out(7098): Invalid db entry for /mnt/sdcard/fsimages3 removed.
02-28 23:13:27.595: I/System.out(7098): 3 : /mnt/sdcard/fsimages2
02-28 23:13:27.603: I/System.out(7098): Invalid db entry for /mnt/sdcard/fsimages2 removed.
02-28 23:13:27.603: I/System.out(7098): 4 : /mnt/sdcard/fsimages1
02-28 23:13:27.603: I/System.out(7098): Invalid db entry for /mnt/sdcard/fsimages1 removed.
02-28 23:13:27.603: I/System.out(7098): 5 : /mnt/sdcard/fsimages
02-28 23:13:46.603: D/NameManager.java(7098): Checking for whatever
02-28 23:13:46.618: I/System.out(7098): 1 : /mnt/sdcard/fsimages87
02-28 23:13:46.618: I/System.out(7098): 2 : /mnt/sdcard/fsimages3
02-28 23:13:46.626: I/System.out(7098): Invalid db entry for /mnt/sdcard/fsimages3 removed.
02-28 23:13:46.626: I/System.out(7098): 3 : /mnt/sdcard/fsimages2
02-28 23:13:46.626: I/System.out(7098): Invalid db entry for /mnt/sdcard/fsimages2 removed.
02-28 23:13:46.626: I/System.out(7098): 4 : /mnt/sdcard/fsimages1
02-28 23:13:46.634: I/System.out(7098): Invalid db entry for /mnt/sdcard/fsimages1 removed.
02-28 23:13:46.634: I/System.out(7098): 5 : /mnt/sdcard/fsimages

What am I doing wrong here?

Upvotes: 0

Views: 3052

Answers (5)

Ashraf Sousa
Ashraf Sousa

Reputation: 256

I had a similar problem and the issue was resolved when I used the primary key in the WHERE clause of the DELETE command. In my case, I had two columns (together) are set as the primary key so I had to use WHERE column1 = 'value1' AND column2 = 'value2'.

This approach can always be doable by running a query command to get the primary key values for the records that need to be deleted.

Upvotes: 0

amikola
amikola

Reputation: 1

If you want to use SQL statement to write database, than use the execSql() method instead of rawQuery()!

The rawQuery() function able to execute a Select, and return a Cursor, that contain the query result, but this is not able to write database.

The execSql() function have no return value! If the success of deleting is important use the delete() method.

Upvotes: 0

CSmith
CSmith

Reputation: 13458

Instead of rawQuery, use the delete() function to effectuate a safe parameterized query:

DbHelper hDbHelper = ...;
SQLiteDatabase hDB;
hDB = hDbHelper.getWritableDatabase();

hDB.delete (DataBaseHelper.VFS_DATABASE_TABLE, 
        String.format("%s = ?",DataBaseHelper.VIRTUAL_SYSTEM_COLUMN_PATH),
        new String[] {pathcursora.getString(0)});

Upvotes: 0

oldingn
oldingn

Reputation: 86

I imagine you need single-quotes around your parameter value, or use a bound variable (preferred).

DELETE FROM vs_table WHERE vspath IS '/mnt/sdcard/fsimages1';

Upvotes: 0

theglauber
theglauber

Reputation: 29665

(Unless you're in auto-commit mode) are you committing your transaction?

Have you checked your SQL statement for accuracy?

Upvotes: 2

Related Questions