• <bdo id='fm6eN'></bdo><ul id='fm6eN'></ul>
      <tfoot id='fm6eN'></tfoot>
    1. <small id='fm6eN'></small><noframes id='fm6eN'>

    2. <legend id='fm6eN'><style id='fm6eN'><dir id='fm6eN'><q id='fm6eN'></q></dir></style></legend>

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

      1. 如果不存在则插入 其他更新?

        INSERT IF NOT EXISTS ELSE UPDATE?(如果不存在则插入 其他更新?)
          <i id='M0xZf'><tr id='M0xZf'><dt id='M0xZf'><q id='M0xZf'><span id='M0xZf'><b id='M0xZf'><form id='M0xZf'><ins id='M0xZf'></ins><ul id='M0xZf'></ul><sub id='M0xZf'></sub></form><legend id='M0xZf'></legend><bdo id='M0xZf'><pre id='M0xZf'><center id='M0xZf'></center></pre></bdo></b><th id='M0xZf'></th></span></q></dt></tr></i><div id='M0xZf'><tfoot id='M0xZf'></tfoot><dl id='M0xZf'><fieldset id='M0xZf'></fieldset></dl></div>

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

          • <legend id='M0xZf'><style id='M0xZf'><dir id='M0xZf'><q id='M0xZf'></q></dir></style></legend>

                • <bdo id='M0xZf'></bdo><ul id='M0xZf'></ul>
                • <tfoot id='M0xZf'></tfoot>
                    <tbody id='M0xZf'></tbody>

                  本文介绍了如果不存在则插入 其他更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  对于经典的如何插入新记录或更新已存在的记录",我找到了一些可能"的解决方案,但我无法让其中任何一个在 SQLite 中工作.

                  I've found a few "would be" solutions for the classic "How do I insert a new record or update one if it already exists" but I cannot get any of them to work in SQLite.

                  我有一个表定义如下:

                  CREATE TABLE Book 
                  ID     INTEGER PRIMARY KEY AUTOINCREMENT,
                  Name   VARCHAR(60) UNIQUE,
                  TypeID INTEGER,
                  Level  INTEGER,
                  Seen   INTEGER
                  

                  我想做的是添加一个具有唯一名称的记录.如果名称已存在,我想修改字段.

                  What I want to do is add a record with a unique Name. If the Name already exists, I want to modify the fields.

                  有人能告诉我怎么做吗?

                  Can somebody tell me how to do this please?

                  推荐答案

                  看看 http://sqlite.org/lang_conflict.html.

                  你想要这样的东西:

                  insert or replace into Book (ID, Name, TypeID, Level, Seen) values
                  ((select ID from Book where Name = "SearchName"), "SearchName", ...);
                  

                  请注意,如果该行已存在于表中,则任何不在插入列表中的字段都将设置为 NULL.这就是 ID 列有一个子选择的原因:在替换情况下,语句会将其设置为 NULL,然后分配一个新的 ID.

                  Note that any field not in the insert list will be set to NULL if the row already exists in the table. This is why there's a subselect for the ID column: In the replacement case the statement would set it to NULL and then a fresh ID would be allocated.

                  如果您想在替换案例中的行保留特定字段值但在插入案例中将该字段设置为 NULL,则也可以使用此方法.

                  This approach can also be used if you want to leave particular field values alone if the row in the replacement case but set the field to NULL in the insert case.

                  例如,假设您想单独保留 Seen:

                  For example, assuming you want to leave Seen alone:

                  insert or replace into Book (ID, Name, TypeID, Level, Seen) values (
                     (select ID from Book where Name = "SearchName"),
                     "SearchName",
                      5,
                      6,
                      (select Seen from Book where Name = "SearchName"));
                  

                  这篇关于如果不存在则插入 其他更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Group by month in SQLite(在 SQLite 中按月分组)
                  Drupal database API query - row.update if exists, else row.insert(Drupal 数据库 API 查询 - row.update 如果存在,否则为 row.insert)
                  How do I create a CSV file from database in Python?(如何从 Python 中的数据库创建 CSV 文件?)
                  SQLite3 and multiple processes(SQLite3 和多个进程)
                  How to import CSV into sqlite using RSqlite?(如何使用 RSqlite 将 CSV 导入 sqlite?)
                  Comparison of database column types in MySQL, PostgreSQL, and SQLite? (Cross-Mapping)(MySQL、PostgreSQL、SQLite中数据库列类型的比较?(交叉映射))
                • <small id='MD9bq'></small><noframes id='MD9bq'>

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

                            <tfoot id='MD9bq'></tfoot>