ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite

前端 未结 14 1875
青春惊慌失措
青春惊慌失措 2020-11-28 08:45

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 alre

14条回答
  •  Happy的楠姐
    2020-11-28 09:36

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

    • 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.

    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.

提交回复
热议问题