检查记录是否存在用于PhoneGap的SQL

时间:2014-02-12 06:26:30

标签: javascript sql cordova

我需要查明是否存在记录,以便我能够更新或删除它。

就我所知,一切都需要交易,选择一个计数是行不通的。

我几乎找不到任何有用的文档,我现有的代码是:

app.database.db.transaction(function (tx) {
    $.each(result.specoffer, function (i, item) {
        if (typeof item.id != 'undefined') {
            var sql = '' +
            'IF EXISTS SELECT id FROM Specials WHERE id = ' + item.id + ' ' +
            'UPDATE Specials SET picture = "' + item.picture + '", startdate = "test", finishdate = "test", mess = "' + item.mess + '", shortmess = "' + item.picture +
            '", shortmess = "' + item.shortmess + '", name = "' + name + '" WHERE id = '                 + item.id + ' ' +
            'ELSE ' +
            'INSERT INTO Specials (id, picture, startdate, finishdate, mess, shortmess, name) VALUES (' +
            item.id + ', "' + item.picture + '", "test", "test", "' + item.mess + '", "'                 + item.shortmess + '", "' + item.name + '")';

            tx.executeSql(sql);
        } 
});

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

您可以直接使用更新或删除

db.transaction(function(tx) {
        tx.executeSql('UPDATE Category SET categoryName="' + categoryName
                + '",categoryIconPath="' + categoryIconPath
                + '",categoryDescription="' + categoryDescription
                + '" WHERE categoryId=' + categoryId + '', [], function(tx,
                result) {

        }, errorCB);

分别放下你的表名和列。这里categoryId就像item.Id一样独特