3

I am trying to do this but this is not working :

tx.executeSql('INSERT INTO MOVIE (id, rate) VALUES(?,?) ON DUPLICATE KEY UPDATE rate=VALUES(rate)',[result.id,score]);

My id is an INT NOT NULL UNIQUE and rate an INT. I think my syntax is wrong... Do you have a solution ?

Thx :) Anthony.

Anthony D'Amato
  • 748
  • 1
  • 6
  • 23

1 Answers1

1

As stated in the Web SQL Database:

User agents must implement the SQL dialect supported by Sqlite 3.6.19.

So my guess is that you are going to face the same issues you get with Sqlite. It seems that SQLite UPSERT - ON DUPLICATE KEY UPDATE is not supported by Sqlite, so I suggest just trying one of the solutions provided in the answers. Maybe something like this would work:

db.transaction(function (tx) {
    tx.executeSql('INSERT OR IGNORE INTO MOVIE VALUES (?, ?)', [result.id,score]);
    tx.executeSql('UPDATE MOVIE SET rate = ? WHERE id = ?', [score,result.id]);
});

See demo

By the way, Web SQL Database is deprecated.

Community
  • 1
  • 1
dreyescat
  • 13,558
  • 5
  • 50
  • 38
  • Thank you for the answer but it doesn't work... It duplicates the data and I want to have only one data with this id. – Anthony D'Amato Jun 09 '15 at 10:41
  • Ok I have found my problem ! My table was : MOVIE ( id unique, rate int , like boolean) and i didn't set the last element. So i have created a table just for the rate ;) Thank you ! – Anthony D'Amato Jun 09 '15 at 10:50