ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite

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

    select * from sqlite_master where type = 'table' and tbl_name = 'TableName' and sql like '%ColumnName%'
    

    Logic: sql column in sqlite_master contains table definition, so it certainly contains string with column name.

    As you are searching for a sub-string, it has its obvious limitations. So I would suggest to use even more restrictive sub-string in ColumnName, for example something like this (subject to testing as '`' character is not always there):

    select * from sqlite_master where type = 'table' and tbl_name = 'MyTable' and sql like '%`MyColumn` TEXT%'
    

提交回复
热议问题