Marc Intes
Marc Intes

Reputation: 737

SQLite statement syntax error near where

I am having problems with the syntax of my SQL statement, the LogCat says the problem is near WHERE. Where did i go wrong?

Here is my code:

var inName = "Test Name";
var inNumber = "Test Number";
db.transaction(function (tx) { tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ( ?, ?) WHERE ? NOT IN (SELECT Name FROM SoccerPlayer)',[ inName, inNumber, inName ] });

Any help or advice will be gladly accepted, this is the last piece of the puzzle for my project. When I am done with this i can already rest. Thanks in advance.

Upvotes: 0

Views: 1062

Answers (1)

laalto
laalto

Reputation: 152807

INSERT does not take a WHERE projection while UPDATE does.

Possibly you wanted either

INSERT INTO SoccerPlayer(Name,Club) VALUES (?, ?)

or

UPDATE SoccerPlayer SET Club=? WHERE Name=?

or in case you want to update in case the Name already exists or insert otherwise,

INSERT OR UPDATE INTO SoccerPlayer(Name,Club) VALUES (?, ?)

where you need the table to have some relevant constraint such as Name TEXT UNIQUE.


Edit:

I want my insert function to ignore the insert if the Name already exists

Then you can make the Name column UNIQUE as above and use INSERT OR IGNORE instead of INSERT OR UPDATE.

Upvotes: 3

Related Questions