Jed
Jed

Reputation: 1043

On duplicate key insert in phonegap

Is there something for inserting into SQLite as the insert on duplicate key in MySQL?

In PhoneGap, I get a SQL syntax error on 'ON DUPLICATE'. I dont want to have to lookup each item before inserting.

My code:

$.each(jsondata, function(i, item) {
    tx.executeSql('INSERT INTO Pricelist2 (id, name,desc,make,price) '+
                  'VALUES ('+jsondata[i].id+', '+
                           '"'+data[i].name+'", '+
                           '"'+jsondata[i].description+'", '+
                           '"'+jsondata[i].make+'", '+
                           '"'+jsondata[i].price+'")'+
                  ' ON DUPLICATE KEY UPDATE '+
                      'desc=\''+jsondata[i].description+'\','+
                      'price=\''+jsondata[i].price+'\';');
    });

Upvotes: 0

Views: 853

Answers (1)

CL.
CL.

Reputation: 180162

You can use only syntax that is actually supported by SQLite.

For this particular problem, INSERT OR REPLACE is likely what you want:

tx.executeSql('INSERT OR REPLACE INTO Pricelist2(id,name,desc,make,price)'+
              'VALUES (?,?,?,?,?)',
              [jsondata[i].id,
               data[i].name,
               jsondata[i].description,
               jsondata[i].make,
               jsondata[i].price]);

(If the record already exists, name and make will also be replaced.)

Upvotes: 3

Related Questions