SQLite修改列

发布于 2024-08-29 22:28:47 字数 245 浏览 3 评论 0原文

我需要修改 SQLite 数据库中的列,但由于数据库已经投入生产,所以我必须以编程方式执行此操作。根据我的研究,我发现为了做到这一点,我必须执行以下操作。

  • 使用新模式创建新表
  • 将数据从旧表复制到新表
  • 删除旧表 将
  • 新表重命名为旧表名称

对于应该相对容易的事情来说,这似乎是一个荒谬的工作量。难道就没有更简单的方法吗?我需要做的就是更改现有列的约束并为其指定默认值。

I need to modify a column in a SQLite database but I have to do it programatically due to the database already being in production. From my research I have found that in order to do this I must do the following.

  • Create a new table with new schema
  • Copy data from old table to new table
  • Drop old table
  • Rename new table to old tables name

That seems like a ridiculous amount of work for something that should be relatively easy. Is there not an easier way? All I need to do is change a constraint on a existing column and give it a default value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尛丟丟 2024-09-05 22:28:47

这是 SQLite 众所周知的缺点之一(ALTER TABLE 不支持 MODIFY COLUMN),但它位于 SQLite 未实现的 SQL 功能列表

编辑:删除了提到它可能在未来版本中受到支持的位,因为页面已更新以表明情况不再如此

That's one of the better-known drawbacks of SQLite (no MODIFY COLUMN support on ALTER TABLE), but it's on the list of SQL features that SQLite does not implement.

edit: Removed bit that mentioned it may being supported in a future release as the page was updated to indicate that is no longer the case

月竹挽风 2024-09-05 22:28:47

如果修改不是太大(例如更改varchar的长度),您可以转储db,手动编辑数据库定义并再次导入回来:

echo '.dump' | sqlite3 test.db > test.dump

然后用文本编辑器打开文件,搜索您想要的定义修改然后:

cat test.dump | sqlite3 new-test.db

If the modification is not too big (e.g. change the length of a varchar), you can dump the db, manually edit the database definition and import it back again:

echo '.dump' | sqlite3 test.db > test.dump

then open the file with a text editor, search for the definition you want to modify and then:

cat test.dump | sqlite3 new-test.db
蓝咒 2024-09-05 22:28:47

正如此处所述,SQLite 并未实现这些功能。

作为旁注,您可以通过使用 select 创建表来完成前两个步骤:

CREATE TABLE tmp_table AS SELECT id, name FROM src_table

As said here, these kind of features are not implemented by SQLite.

As a side note, you could make your two first steps with a create table with select:

CREATE TABLE tmp_table AS SELECT id, name FROM src_table
落日海湾 2024-09-05 22:28:47

当我运行“CREATE TABLE tmp_table AS SELECT id, name FROM src_table”时,我丢失了所有列类型格式(例如,时间字段变成整数字段

正如最初所述,似乎应该更容易,但这是我所做的修复我遇到了这个问题,因为我想更改列中的 Not Null 字段,而 Sqlite 并没有真正帮助我

使用“SQLite Manager”Firefox 插件浏览器(我通过复制创建了新表)。旧的创建语句,进行修改并执行它,然后为了复制数据,我只需突出显示这些行,右键单击“将行复制为 SQL”,将“someTable”替换为我的表名称,然后执行了SQL。

When I ran "CREATE TABLE tmp_table AS SELECT id, name FROM src_table", I lost all the column type formatting (e.g., time field turned into a integer field

As initially stated seems like it should be easier, but here is what I did to fix. I had this problem b/c I wanted to change the Not Null field in a column and Sqlite doesnt really help there.

Using the 'SQLite Manager' Firefox addon browser (use what you like). I created the new table by copying the old create statement, made my modification, and executed it. Then to get the data copied over, I just highlighted the rows, R-click 'Copy Row(s) as SQL', replaced "someTable" with my table name, and executed the SQL.

天气好吗我好吗 2024-09-05 22:28:47

Various good answers already given to this question, but I also suggest taking a look at the sqlite.org page on ALTER TABLE which covers this issue in some detail: What (few) changes are possible to columns (RENAME|ADD|DROP) but also detailed workarounds for other operations in the section Making Other Kinds Of Table Schema Changes and background info in Why ALTER TABLE is such a problem for SQLite. In particular the workarounds point out some pitfalls when working with more complex tables and explain how to make changes safely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文