且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-10 21:05:04

我有一个 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.