SQLite - UPSERT *not* INSERT or REPLACE

后端 未结 18 2611
猫巷女王i
猫巷女王i 2020-11-21 23:53

http://en.wikipedia.org/wiki/Upsert

Insert Update stored proc on SQL Server

Is there some clever way to do this in SQLite that I have not thought of?

相关标签:
18条回答
  • 2020-11-22 00:09

    Assuming three columns in the table: ID, NAME, ROLE


    BAD: This will insert or replace all columns with new values for ID=1:

    INSERT OR REPLACE INTO Employee (id, name, role) 
      VALUES (1, 'John Foo', 'CEO');
    

    BAD: This will insert or replace 2 of the columns... the NAME column will be set to NULL or the default value:

    INSERT OR REPLACE INTO Employee (id, role) 
      VALUES (1, 'code monkey');
    

    GOOD: Use SQLite On conflict clause UPSERT support in SQLite! UPSERT syntax was added to SQLite with version 3.24.0!

    UPSERT is a special syntax addition to INSERT that causes the INSERT to behave as an UPDATE or a no-op if the INSERT would violate a uniqueness constraint. UPSERT is not standard SQL. UPSERT in SQLite follows the syntax established by PostgreSQL.

    GOOD but tendous: This will update 2 of the columns. When ID=1 exists, the NAME will be unaffected. When ID=1 does not exist, the name will be the default (NULL).

    INSERT OR REPLACE INTO Employee (id, role, name) 
      VALUES (  1, 
                'code monkey',
                (SELECT name FROM Employee WHERE id = 1)
              );
    

    This will update 2 of the columns. When ID=1 exists, the ROLE will be unaffected. When ID=1 does not exist, the role will be set to 'Benchwarmer' instead of the default value.

    INSERT OR REPLACE INTO Employee (id, name, role) 
      VALUES (  1, 
                'Susan Bar',
                COALESCE((SELECT role FROM Employee WHERE id = 1), 'Benchwarmer')
              );
    
    0 讨论(0)
  • 2020-11-22 00:10

    Having just read this thread and been disappointed that it wasn't easy to just to this "UPSERT"ing, I investigated further...

    You can actually do this directly and easily in SQLITE.

    Instead of using: INSERT INTO

    Use: INSERT OR REPLACE INTO

    This does exactly what you want it to do!

    0 讨论(0)
  • 2020-11-22 00:11

    Updates from Bernhardt:

    You can indeed do an upsert in SQLite, it just looks a little different than you are used to. It would look something like:

    INSERT INTO table_name (id, column1, column2) 
    VALUES ("youruuid", "value12", "value2")
    ON CONFLICT(id) DO UPDATE 
    SET column1 = "value1", column2 = "value2"
    
    0 讨论(0)
  • 2020-11-22 00:12

    This answer has be updated and so the comments below no longer apply.

    2018-05-18 STOP PRESS.

    UPSERT support in SQLite! UPSERT syntax was added to SQLite with version 3.24.0 (pending) !

    UPSERT is a special syntax addition to INSERT that causes the INSERT to behave as an UPDATE or a no-op if the INSERT would violate a uniqueness constraint. UPSERT is not standard SQL. UPSERT in SQLite follows the syntax established by PostgreSQL.

    alternatively:

    Another completely different way of doing this is: In my application I set my in memory rowID to be long.MaxValue when I create the row in memory. (MaxValue will never be used as an ID you will won't live long enough.... Then if rowID is not that value then it must already be in the database so needs an UPDATE if it is MaxValue then it needs an insert. This is only useful if you can track the rowIDs in your app.

    0 讨论(0)
  • 2020-11-22 00:16

    If someone wants to read my solution for SQLite in Cordova, I got this generic js method thanks to @david answer above.

    function    addOrUpdateRecords(tableName, values, callback) {
    get_columnNames(tableName, function (data) {
        var columnNames = data;
        myDb.transaction(function (transaction) {
            var query_update = "";
            var query_insert = "";
            var update_string = "UPDATE " + tableName + " SET ";
            var insert_string = "INSERT INTO " + tableName + " SELECT ";
            myDb.transaction(function (transaction) {
                // Data from the array [[data1, ... datan],[()],[()]...]:
                $.each(values, function (index1, value1) {
                    var sel_str = "";
                    var upd_str = "";
                    var remoteid = "";
                    $.each(value1, function (index2, value2) {
                        if (index2 == 0) remoteid = value2;
                        upd_str = upd_str + columnNames[index2] + "='" + value2 + "', ";
                        sel_str = sel_str + "'" + value2 + "', ";
                    });
                    sel_str = sel_str.substr(0, sel_str.length - 2);
                    sel_str = sel_str + " WHERE NOT EXISTS(SELECT changes() AS change FROM "+tableName+" WHERE change <> 0);";
                    upd_str = upd_str.substr(0, upd_str.length - 2);
                    upd_str = upd_str + " WHERE remoteid = '" + remoteid + "';";                    
                    query_update = update_string + upd_str;
                    query_insert = insert_string + sel_str;  
                    // Start transaction:
                    transaction.executeSql(query_update);
                    transaction.executeSql(query_insert);                    
                });
            }, function (error) {
                callback("Error: " + error);
            }, function () {
                callback("Success");
            });
        });
    });
    }
    

    So, first pick up the column names with this function:

    function get_columnNames(tableName, callback) {
    myDb.transaction(function (transaction) {
        var query_exec = "SELECT name, sql FROM sqlite_master WHERE type='table' AND name ='" + tableName + "'";
        transaction.executeSql(query_exec, [], function (tx, results) {
            var columnParts = results.rows.item(0).sql.replace(/^[^\(]+\(([^\)]+)\)/g, '$1').split(','); ///// RegEx
            var columnNames = [];
            for (i in columnParts) {
                if (typeof columnParts[i] === 'string')
                    columnNames.push(columnParts[i].split(" ")[0]);
            };
            callback(columnNames);
        });
    });
    }
    

    Then build the transactions programmatically.

    "Values" is an array you should build before and it represents the rows you want to insert or update into the table.

    "remoteid" is the id I used as a reference, since I'm syncing with my remote server.

    For the use of the SQLite Cordova plugin, please refer to the official link

    0 讨论(0)
  • 2020-11-22 00:16

    This method remixes a few of the other methods from answer in for this question and incorporates the use of CTE (Common Table Expressions). I will introduce the query then explain why I did what I did.

    I would like to change the last name for employee 300 to DAVIS if there is an employee 300. Otherwise, I will add a new employee.

    Table Name: employees Columns: id, first_name, last_name

    The query is:

    INSERT OR REPLACE INTO employees (employee_id, first_name, last_name)
    WITH registered_employees AS ( --CTE for checking if the row exists or not
        SELECT --this is needed to ensure that the null row comes second
            *
        FROM (
            SELECT --an existing row
                *
            FROM
                employees
            WHERE
                employee_id = '300'
    
            UNION
    
            SELECT --a dummy row if the original cannot be found
                NULL AS employee_id,
                NULL AS first_name,
                NULL AS last_name
        )
        ORDER BY
            employee_id IS NULL --we want nulls to be last
        LIMIT 1 --we only want one row from this statement
    )
    SELECT --this is where you provide defaults for what you would like to insert
        registered_employees.employee_id, --if this is null the SQLite default will be used
        COALESCE(registered_employees.first_name, 'SALLY'),
        'DAVIS'
    FROM
        registered_employees
    ;
    

    Basically, I used the CTE to reduce the number of times the select statement has to be used to determine default values. Since this is a CTE, we just select the columns we want from the table and the INSERT statement uses this.

    Now you can decide what defaults you want to use by replacing the nulls, in the COALESCE function with what the values should be.

    0 讨论(0)
提交回复
热议问题