SQLite updating row

2362 views sql
3

I am developing a mobile application on cordova with a SQLite database.

The application receives data from an external source via JSONP and writes data to the database.

I need to use the sql query to determine if there is an entry in the table with such an id, and if there is, then rewrite the row, and if not, add a new one.

At the moment, the data writing function looks like this:

    addNews: function (id, title, date, content) {
    databaseHandler.db.transaction(
        function (tx) {
            tx.executeSql(
                "insert into news(id, title, date, content) values(?, ?, ?, ?)",
                [id, title, date, content],
                function (tx, results) { },
                function (tx, error) {
                    console.log("add news error: " + error.message);
                }
            );
        },
        function (error) {
        },
        function () {
        }
    );
}

var url = "http://cp35240-wordpress.tw1.ru/wp-content/plugins/plugin/news.js";
var script = document.createElement('script');
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);

function news(data) {
    var id = data.id;
    var title = data.title;
    var date = data.date;
    var content = data.content;  
    newsHandler.addNews(id, title, date, content);   
}

answered question

1 Answer

1

You can make useof INSERT OR REPLACE

Syntax

INSERT OR REPLACE INTO TABLE (column_list) 
VALUES (value_list);

So here you can use

INSERT OR REPLACE INTO news(id, title, date, content) values(?, ?, ?, ?)

posted this

Have an answer?

JD

Please login first before posting an answer.