如何使用 FMDB 删除特定 table 的列?

How to delete specific table's column using FMDB?

我正在尝试使用 FMDB

从 Persons 中删除列 last_name
let query = "ALTER TABLE Persons DROP COLUMN last_name;"
try FMDBHelper.database.executeUpdate(query, values: nil)

但是出现错误

DB Error: 1 "near "DROP": syntax error".

sqlite 不支持 ALTER TABLE 中的 DROP COLUMN。

您只能重命名 table 和添加列。

如果您需要删除列,创建一个新的 table,将数据复制到那里,删除旧的 table 并将 table 重命名为它的预期名称。

参考:http://www.sqlite.org/lang_altertable.html

请注意,我已标记您的问题可能重复,我会提供一个答案以使其更清楚。

我认为您缺少 一点,即:FMDB 是(如他们的回购描述中所述):

This is an Objective-C wrapper around SQLite

请记住,由于 FMDB 是建立在 SQLite 之上的,因此它不是库本身的限制;它与 SQLite ALTER TABLE 的工作方式有关。

SQLite ALTER TABLE语句仅限于重命名一个table和add一个新的列到想要的table:

SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table.

http://www.sqlite.org/lang_altertable.html


实现您的目标:

您可以查看 Delete column from SQLite table 的答案。