1. <legend id='2Vzfn'><style id='2Vzfn'><dir id='2Vzfn'><q id='2Vzfn'></q></dir></style></legend>
      <i id='2Vzfn'><tr id='2Vzfn'><dt id='2Vzfn'><q id='2Vzfn'><span id='2Vzfn'><b id='2Vzfn'><form id='2Vzfn'><ins id='2Vzfn'></ins><ul id='2Vzfn'></ul><sub id='2Vzfn'></sub></form><legend id='2Vzfn'></legend><bdo id='2Vzfn'><pre id='2Vzfn'><center id='2Vzfn'></center></pre></bdo></b><th id='2Vzfn'></th></span></q></dt></tr></i><div id='2Vzfn'><tfoot id='2Vzfn'></tfoot><dl id='2Vzfn'><fieldset id='2Vzfn'></fieldset></dl></div>
        • <bdo id='2Vzfn'></bdo><ul id='2Vzfn'></ul>

      1. <small id='2Vzfn'></small><noframes id='2Vzfn'>

        <tfoot id='2Vzfn'></tfoot>

        SQLite UPSERT/更新或插入

        SQLite UPSERT / UPDATE OR INSERT(SQLite UPSERT/更新或插入)

              <bdo id='tA90D'></bdo><ul id='tA90D'></ul>
                <tbody id='tA90D'></tbody>

              <small id='tA90D'></small><noframes id='tA90D'>

              • <legend id='tA90D'><style id='tA90D'><dir id='tA90D'><q id='tA90D'></q></dir></style></legend>
                <tfoot id='tA90D'></tfoot>

                1. <i id='tA90D'><tr id='tA90D'><dt id='tA90D'><q id='tA90D'><span id='tA90D'><b id='tA90D'><form id='tA90D'><ins id='tA90D'></ins><ul id='tA90D'></ul><sub id='tA90D'></sub></form><legend id='tA90D'></legend><bdo id='tA90D'><pre id='tA90D'><center id='tA90D'></center></pre></bdo></b><th id='tA90D'></th></span></q></dt></tr></i><div id='tA90D'><tfoot id='tA90D'></tfoot><dl id='tA90D'><fieldset id='tA90D'></fieldset></dl></div>
                  本文介绍了SQLite UPSERT/更新或插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要对 SQLite 数据库执行 UPSERT/INSERT OR UPDATE.

                  有 INSERT OR REPLACE 命令,它在很多情况下都很有用.但是,如果您想因为外键而保留带有自动增量的 id,则它不起作用,因为它删除了该行,创建了一个新行,因此该新行有一个新 ID.

                  这就是表格:

                  players - (id 的主键,user_name 唯一)

                  <代码>|身份证 |用户名 |年龄 |------------------------------|1982 |约翰尼|23 ||1983 |史蒂文 |29 ||1984 |佩佩|40 |

                  解决方案

                  这是一个迟到的答案.从2018年6月4日发布的SQLIte 3.24.0开始,终于支持UPSERTPostgreSQL 语法后的子句.

                  INSERT INTO 玩家(用户名,年龄)值('史蒂文',32)冲突(用户名)做 UPDATE SET age=excluded.age;

                  <块引用>

                  注意:对于那些必须使用早于 3.24.0 版本的 SQLite,请参考下面的这个答案(由我@MarqueIV 发布).

                  但是,如果您确实可以选择升级,则强烈建议这样做,因为与我的解决方案不同,此处发布的解决方案在单个语句中实现了所需的行为.此外,您还可以获得更新版本通常附带的所有其他功能、改进和错误修复.

                  I need to perform UPSERT / INSERT OR UPDATE against a SQLite Database.

                  There is the command INSERT OR REPLACE which in many cases can be useful. But if you want to keep your id's with autoincrement in place because of foreign keys, it does not work since it deletes the row, creates a new one and consequently this new row has a new ID.

                  This would be the table:

                  players - (primary key on id, user_name unique)

                  |  id   | user_name |  age   |
                  ------------------------------
                  |  1982 |   johnny  |  23    |
                  |  1983 |   steven  |  29    |
                  |  1984 |   pepee   |  40    |
                  

                  解决方案

                  This is a late answer. Starting from SQLIte 3.24.0, released on June 4, 2018, there is finally a support for UPSERT clause following PostgreSQL syntax.

                  INSERT INTO players (user_name, age)
                    VALUES('steven', 32) 
                    ON CONFLICT(user_name) 
                    DO UPDATE SET age=excluded.age;
                  

                  Note: For those having to use a version of SQLite earlier than 3.24.0, please reference this answer below (posted by me, @MarqueIV).

                  However if you do have the option to upgrade, you are strongly encouraged to do so as unlike my solution, the one posted here achieves the desired behavior in a single statement. Plus you get all the other features, improvements and bug fixes that usually come with a more recent release.

                  这篇关于SQLite UPSERT/更新或插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Splitting the string in sql server(在 sql server 中拆分字符串)
                  create table with sequence.nextval in oracle(在oracle中用sequence.nextval创建表)
                  SQL recursive query on self referencing table (Oracle)(自引用表 (Oracle) 上的 SQL 递归查询)
                  Composite Primary key vs additional quot;IDquot; column?(复合主键与附加“ID柱子?)
                  How to check if a database exists in SQL Server?(如何检查数据库是否存在于 SQL Server 中?)
                  How to determine total number of open/active connections in ms sql server 2005(如何确定 ms sql server 2005 中打开/活动连接的总数)
                    <tbody id='m4XwC'></tbody>
                  1. <small id='m4XwC'></small><noframes id='m4XwC'>

                        <bdo id='m4XwC'></bdo><ul id='m4XwC'></ul>
                        <i id='m4XwC'><tr id='m4XwC'><dt id='m4XwC'><q id='m4XwC'><span id='m4XwC'><b id='m4XwC'><form id='m4XwC'><ins id='m4XwC'></ins><ul id='m4XwC'></ul><sub id='m4XwC'></sub></form><legend id='m4XwC'></legend><bdo id='m4XwC'><pre id='m4XwC'><center id='m4XwC'></center></pre></bdo></b><th id='m4XwC'></th></span></q></dt></tr></i><div id='m4XwC'><tfoot id='m4XwC'></tfoot><dl id='m4XwC'><fieldset id='m4XwC'></fieldset></dl></div>
                      • <legend id='m4XwC'><style id='m4XwC'><dir id='m4XwC'><q id='m4XwC'></q></dir></style></legend>
                        <tfoot id='m4XwC'></tfoot>