ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite

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.

Leave a Comment