0

I am trying to delete column last_name from Persons using FMDB,

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

But comes with error

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

Amit
  • 556
  • 1
  • 7
  • 24
  • Possible duplicate of [How to delete or add column in SQLITE?](https://stackoverflow.com/questions/8442147/how-to-delete-or-add-column-in-sqlite) – Ahmad F Aug 08 '17 at 07:17
  • @AhmadF FYI i am trying in SWIFT-FMDB. – Amit Aug 08 '17 at 07:20

2 Answers2

2

sqlite does not support DROP COLUMN in ALTER TABLE.

You can only rename tables and add columns.

If you need to remove columns, create a new table, copy the data there, drop the old table and rename the table to its intented name.

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

Abhishek Jain
  • 4,557
  • 2
  • 32
  • 31
0

Please note that I flagged that your question could be duplicated, I will provide an answer to make it more clear.

I think that you are missing a point, which is: The FMDB is (as mentioned in their repo description):

This is an Objective-C wrapper around SQLite

Keep in mind that since FMDB is built on top of SQLite, it is not a limitation from the library itself; it is related to how SQLite ALTER TABLE works.

The SQLite ALTER TABLE statement is limited to rename a table and add a new column to the desired 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


For achieving what are you looking for:

You could check the answers of Delete column from SQLite table.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143