Check if a column exists in SQLite

前端 未结 15 525
我寻月下人不归
我寻月下人不归 2020-12-08 07:02

I need to check to see if a column exists and if it doesn\'t exist add it. From my research it looks like sqlite doesn\'t support IF statements and case statement should be

15条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 07:22

    You did not specify a language, so assuming it's not pure sql, you can check for errors on column querying:

    SELECT col FROM table;
    

    if you get an error so you know the column is not there (assuming you know the table exists, anyway you have the "IF NOT EXISTS" for this), otherwise the column exists and then you can alter the table accordingly.

提交回复
热议问题