<tfoot id='GDXir'></tfoot>

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

  1. <legend id='GDXir'><style id='GDXir'><dir id='GDXir'><q id='GDXir'></q></dir></style></legend>

        <bdo id='GDXir'></bdo><ul id='GDXir'></ul>

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

      如果 SQLite 中不存在,则更改表添加列

      时间:2023-06-24
      1. <i id='IEK5S'><tr id='IEK5S'><dt id='IEK5S'><q id='IEK5S'><span id='IEK5S'><b id='IEK5S'><form id='IEK5S'><ins id='IEK5S'></ins><ul id='IEK5S'></ul><sub id='IEK5S'></sub></form><legend id='IEK5S'></legend><bdo id='IEK5S'><pre id='IEK5S'><center id='IEK5S'></center></pre></bdo></b><th id='IEK5S'></th></span></q></dt></tr></i><div id='IEK5S'><tfoot id='IEK5S'></tfoot><dl id='IEK5S'><fieldset id='IEK5S'></fieldset></dl></div>
        <legend id='IEK5S'><style id='IEK5S'><dir id='IEK5S'><q id='IEK5S'></q></dir></style></legend>

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

            • <small id='IEK5S'></small><noframes id='IEK5S'>

              <tfoot id='IEK5S'></tfoot>
                本文介绍了如果 SQLite 中不存在,则更改表添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我们最近需要向一些现有的 SQLite 数据库表添加列.这可以通过 ALTER TABLE ADD COLUMN 来完成.当然,如果表已经被改变了,我们想不管它.不幸的是,SQLite 不支持 ALTER TABLE 上的 IF NOT EXISTS 子句.

                We've recently had the need to add columns to a few of our existing SQLite database tables. This can be done with ALTER TABLE ADD COLUMN. Of course, if the table has already been altered, we want to leave it alone. Unfortunately, SQLite doesn't support an IF NOT EXISTS clause on ALTER TABLE.

                我们目前的解决方法是执行 ALTER TABLE 语句并忽略任何重复列名"错误,就像 这个 Python 示例(但在 C++ 中).

                Our current workaround is to execute the ALTER TABLE statement and ignore any "duplicate column name" errors, just like this Python example (but in C++).

                然而,我们通常设置数据库模式的方法是使用包含 CREATE TABLE IF NOT EXISTSCREATE INDEX IF NOT EXISTS 语句的 .sql 脚本,它们可以使用 sqlite3_execsqlite3 命令行工具执行.我们不能将 ALTER TABLE 放在这些脚本文件中,因为如果该语句失败,则不会执行其后的任何内容.

                However, our usual approach to setting up database schemas is to have a .sql script containing CREATE TABLE IF NOT EXISTS and CREATE INDEX IF NOT EXISTS statements, which can be executed using sqlite3_exec or the sqlite3 command-line tool. We can't put ALTER TABLE in these script files because if that statement fails, anything after it won't be executed.

                我想将表定义放在一个地方,而不是在 .sql 和 .cpp 文件之间拆分.有没有办法在纯 SQLite SQL 中为 ALTER TABLE ADD COLUMN IF NOT EXISTS 编写解决方法?

                I want to have the table definitions in one place and not split between .sql and .cpp files. Is there a way to write a workaround to ALTER TABLE ADD COLUMN IF NOT EXISTS in pure SQLite SQL?

                推荐答案

                我有一个 99% 的纯 SQL 方法.这个想法是对您的架构进行版本控制.您可以通过两种方式执行此操作:

                I have a 99% pure SQL method. The idea is to version your schema. You can do this in two ways:

                • 使用user_version"编译指示命令(PRAGMA user_version) 存储数据库架构版本的增量编号.

                • Use the 'user_version' pragma command (PRAGMA user_version) to store an incremental number for your database schema version.

                将您的版本号存储在您自己定义的表中.

                Store your version number in your own defined table.

                这样,当软件启动时,它可以检查数据库架构,如果需要,运行您的ALTER TABLE 查询,然后增加存储的版本.这比盲目"尝试各种更新要好得多,尤其是在您的数据库多年来增长和更改了几次的情况下.

                In this way, when the software is started, it can check the database schema and, if needed, run your ALTER TABLE query, then increment the stored version. This is by far better than attempting various updates "blind", especially if your database grows and changes a few times over the years.

                这篇关于如果 SQLite 中不存在,则更改表添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:sqlite alter table 在单个语句中添加多个列 下一篇:创建临时表前先检查临时表是否存在,如果存在则删除

                相关文章

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

                <legend id='KkU4t'><style id='KkU4t'><dir id='KkU4t'><q id='KkU4t'></q></dir></style></legend>

                • <bdo id='KkU4t'></bdo><ul id='KkU4t'></ul>

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

                  <tfoot id='KkU4t'></tfoot>