Ok, so I'm trying to make my database delete an entire row if two conditions are fulfilled. I've tried using the delete(String tableName, String whereClause, String[] whereArgs) method as provided from the SDK
However, a row is deleted only if I put in ONE where clause, such as this:
mDatabase.delete(ItemTable.NAME, ItemTable.cols.NAME + " = ?",
new String[] {latestItem.getName()});
Wherein attempting to put in multiple WHERE clauses breaks the whole thing
mDatabase.delete(ItemTable.NAME,
ItemTable.cols.NAME + " = ? AND " +
ItemTable.cols.PRICE + " = ?",
new String[] {latestItem.getName(),
String.valueOf(latestItem.getPrice())});
I am completely out of ideas on how to tackle this. Any and all help is appreciated.
Ok guys, I figured out what was wrong.
Turns out the condition for checking the item name was returning TRUE but the item price condition wasn't. Apparently there was a mismatch with the sqlite data affinity of the itemprice() data.
I fixed this by forcing all column data affinities to be TEXT and storing my item price as a String through String.valueOf(getItemprice()). Everything works this way.
Thanks though for all your help!
Related
I have a very strange bug in my code. Add/Remove to SqlLiteDb works well. But there is one entry I can't remove. I really have no idea how this is possible.
I add a value like this:
ContentValues values = new ContentValues();
values.put(COLUMN_ID, id);
getWritableDatabase().insert(TABLE_SUBSCRIPTIONS, null, values);
And remove a value like this:
getWritableDatabase().delete(TABLE_SUBSCRIPTIONS, COLUMN_ID + " = " + id, null);
If I call
addEntry("43.05071");
removeEntry("43.05071");
the db is empty.
But after
addEntry("43.06600");
removeEntry("43.06600");
43.06600 is still in the db.
When I execute
getWritableDatabase().execSQL("delete from "+ TABLE_SUBSCRIPTIONS)
the table is empty as expected even if it contained 43.06600.
I doubt that the ID per se causes the problem. But do you have any ideas what I could have overlooked?
Thanks in advance.
Update: I updated my remote database and changed the id from 43.06600 to 43.05072. No it works for the object which had the 43.06600 id. How is that possible?
I am building an Android app that uses a SQLite database.
For this one task I have to run a query that looks like this:
SELECT item.id, item.price, t1.quantity
FROM item, (SELECT id, price
FROM list
WHERE list.state = 'sold') t1
WHERE item.id = t1.id
So far, I have tried:
Cursor c = resolver.query(uriRawQuery, null, selection, null, null)
where uriRawQuery is used to tell the ContentProvider that it should perform a db.rawQuery(selection, null) and selection is a string similar to the query above.
The problem is no data is returned into the Cursor. When I call c.moveToFirst() I get false.
The weird thing is that if I open the database file in SQLite Manager and run the exact same query I get results.
I know I can modify the query to make a join between the original list and item tables but I find it to be less efficient that way.
Any ideas would be very appreciated as I have spent too man hours on this already.
EDIT
I know what a join is, what I said is that it is a lot more efficient if I do it like this instead of using the entire list table.
I forgot a very important aspect
The WHERE clause looks like
" WHERE list.state = 'sold' and list.name like '" + arg + "%'"
where arg is a string.
I managed to solve the problem, I still don't know why this was happening but at least I got the Cursor to actually select the rows.
After many trials I thought about ditching the syntax above and write this instead:
" WHERE list.state = 'sold' and list.name like ? "
and move the argument in
selectionArgs = new String[]{arg + "%"}
I am going to wait a while before accepting the answer, in case someone provides an explanation as to why even though both queries look exactly the same they get different results.
I want to delete all rows in a table with a specific ID (not primary key).
I have tested two different methods, but they only remove the first row it finds with the specific ID:
db.delete(CalendarTable.TABLE_NAME, "repeat_group="+repeatGroup, null);
and
db.delete(CalendarTable.TABLE_NAME, "repeat_group=?", new String[]{Integer.toString(repeatGroup)});
None of these methods works, how can I remove ALL rows in a table with this specific ID?
Thanks in advance!
UPDATE:
Lol, the method above did work! It was just me the stupid one that called the my own method delete() instead of deleteRepeatGroup(), guess I'm too tired!
Anyways, thank you guys for taking your time.
If everything else fails, you can try the following. Get all rows in the table with the ID you are trying to delete and save the rowID's in an array. Then iterate over the array and delete each row.
I hope this works as expected
you can use
String urQuery = "delete from tablename where Id in ("
+ Id + ")";
here Id may have all ids separated by comma ex. "id1,id2".
I have a listview and i am getting the data from sqlite database. My problem is to delete a row which user selected it from listview. I can delete all table by
dbConn.delete("Restobj", null,null);
But i cant delete a single row which is selected from listview.
Please Help
You essentially need to get the row id from the selected ListView item. Using the row id you can easily delete that row:
String where = "_id = " + _id;
db.delete(TABLE_NAME, where, null);
After deleting the row item make sure you get the ListView adapter cursor and do a requery. If you don't you will not see the updated table, hence updated ListView.
Make use of those other two parameters to the delete method. Take a look at the API documentation for more information.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#delete%28java.lang.String,%20java.lang.String,%20java.lang.String[]%29
Pass in something other than null.
Also, try searching on stackoverflow and/or Google for this topic. The answers are plentiful.
You need to supply the appropriate values to the database object. I'm assuming that dbConn is an instance of a database object. If that is the case, you can pass in dbConn.delete() with 3 arguments. The first argument is the table name. The second is the whereClause. This is something similar to:
"id = " + objectDatabaseId
The final variable in this case you can leave blank. The end result is something like:
String whereClause = "id = " + objectDatabaseId;
dbConn.delete("Restobj", whereClause, null);
As a side note, it's better to use constants when referring to table names and table columns as apposed to "Restobj" you should have something like RestObject.TABLE_NAME where the constant is defined as a static final String inside of the RestObject.
-Randall
I am trying to learn the SQL Database stuff for SQLite using the android. I have seen a couple examples of the Queries....
I have a two part question about sqlite queries in android.
Part 1
Say I want to delete something. and I use the following Query.
db.delete(MY_DB_TABLE, "CustomerName = ?", new String[] { customerName });
what would happen if the Customer name had a bad character in it.
For example. If I use the following Query
db.execSQL("delete from " + MY_DB_TABLE +
" where customername = '" + customerName + "';");
and say for this example the name of my customer was "Arby's".
That query would blow up because the ' is a special character and the query would not be formatted correctly.
Part 2
does this format allow me to specify as many paramaters as I want.
Example:
db.delete(MYTABLE, "val1 = ? and val2 != ?", new String[] { "test", "test2" } );
Please refer to my post here:
Storing Lists to A Database, and Retrieving Them All Together : Android
and short answer to your question, yes.
Each '?' means that an argument will be expected, so for each '?' you WILL have an exact number of arguments to pass in unless you want an exception :) !