ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite

前端 未结 14 1904
青春惊慌失措
青春惊慌失措 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:27

    You can alternatively use the CASE-WHEN TSQL statement in combination with pragma_table_info to know if a column exists:

    select case(CNT) 
        WHEN 0 then printf('not found')
        WHEN 1 then printf('found')
        END
    FROM (SELECT COUNT(*) AS CNT FROM pragma_table_info('myTableName') WHERE name='columnToCheck') 
    

提交回复
热议问题