ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite

前端 未结 14 1903
青春惊慌失措
青春惊慌失措 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条回答
  •  伪装坚强ぢ
    2020-11-28 09:41

    I come up with this query

    SELECT CASE (SELECT count(*) FROM pragma_table_info(''product'') c WHERE c.name = ''purchaseCopy'') WHEN 0 THEN ALTER TABLE product ADD purchaseCopy BLOB END
    
    • Inner query will return 0 or 1 if column exists.
    • Based on the result, alter the column

提交回复
热议问题