SQLite如果列存在
问题描述:
我想知道是否有一个很好的IF NOT EXISTS检查SQLite列和索引,或者我需要带回整个数据库模式并验证?
I was wondering if there is a nice IF NOT EXISTS for checking columns and indexes in SQLite, or do I need to bring back the entire database schema and validate against that?
答
是的,sqlite支持以下语法: CREATE INDEX IF NOT EXISTS ...
Yes the following syntax is supported in sqlite: CREATE INDEX IF NOT EXISTS ...
要检查列的存在,你可以简单地尝试做一些 SELECT col from TABLE
。如果它不返回错误,您的表包含 col
。
To check existence of a column you could simply try to do something like SELECT col from TABLE
. If it does not return an error your table contains col
.