1

I know there are already too many questions about this, and believe me I've done my part reading the answers to find a solution. Unfortunately, I can't find my mistake

Here's my code:

SQLiteDatabase db = getWritableDatabase();

ContentValues values = new ContentValues();
values.put("eat_it", 1);
values.put("expired", 1);
String[] args = {food.name, "0"};

db.update("foods", values, "name=? AND expired=?", args);
db.close();

What I want to do:

Find a record in the table foods WHERE the value of column name = food.name and the value of column expired = 0. If found, SET the record's eat_it column to 1 and expired column to 1. If no record found, don't do anything

Here's my create table syntax:

CREATE TABLE IF NOT EXISTS foods (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    ...
    eat_it INTEGER DEFAULT 0,
    expired INTEGER DEFAULT 0
);

Here's the error message:

...
 Caused by: android.database.sqlite.SQLiteException: near "WHERE": syntax error (code 1): , while compiling: UPDATE foods SET expired=?,eat_it=? WHERE name ='Taco Salad'WHERE expired = 0
...

Thanks for your help

====UPDATE====

Apparently after I restart my computer it worked. I think my emulator was not working properly/ still has the old code. Thanks everyone for helping

2
  • it looks like this is not the code that caused this exception Commented Jul 30, 2015 at 23:55
  • @njzk2 you're right. I'm so sorry, I should've restarted my computer before asking it here Commented Jul 31, 2015 at 0:08

2 Answers 2

2

Error message returned per your post

near "WHERE": syntax error (code 1): , while compiling: UPDATE foods SET expired=?,eat_it=? WHERE name ='Taco Salad'WHERE expired = 0

It's clear from the error statement that you have two WHERE clause in your query and so the error.

UPDATE foods SET expired=?,eat_it=? 
WHERE name ='Taco Salad'
WHERE expired = 0 <-- Here

Your UPDATE statement should look like

UPDATE foods SET expired=?,eat_it=? 
WHERE name ='Taco Salad'
AND expired = 0 
Sign up to request clarification or add additional context in comments.

1 Comment

I figured that too. But my third argument is : "name=? AND expired=?". I don't know why but the AND is not showing in the error message
0

You can use the where statement without using the 'whereArgs' like this:

db.update("foods", values, "name="+food.name+" AND expired=0", null);

Also, since you're updating internal values, you can also try a simple query:

db.rawquery("UPDATE foods SET eat_it=1, expired=1 WHERE name="+food.name+" AND expired=0");

Although there should be nothing wrong with what you wrote there, you can try those two options to make it work.

4 Comments

why would you do that? why would you suggest to not use the tools at our disposal? and more to the point, why would you think it would work any better? (fyi, your queries obviously fail because food.name is a string)
Because it's obviously not working properly, right? Besides, if he can't make it work even with a raw query, then he knows problem is not in the statement, but in the vars. Hum, yeah? food.name is a string. Functions expects string... what's the problem?
exactly. it's not working properly, yet nothing seems wrong, and looking at the error tells us that the code is not related to the error. There is no point in fiddling around trying different methods of obtaining the same result if what the error is is not clear in the first place
Alright, you have a point there. But I still think that trying other methods of getting the same result, and getting the same exception in return does help to find that the problem was with the compiler/emulator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.