检查 SQLite 中是否存在列 [英] Check if a column exists in SQLite

查看:122
本文介绍了检查 SQLite 中是否存在列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查列是否存在,如果不存在则添加它.从我的研究看来,sqlite 不支持 IF 语句,而应该使用 case 语句.

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 used instead.

这是我目前所拥有的:

SELECT CASE WHEN exists(select * from qaqc.columns where Name = "arg" and Object_ID = Object_ID("QAQC_Tasks")) = 0 THEN ALTER TABLE QAQC_Tasks ADD arg INT DEFAULT(0);

但我收到错误消息:靠近ALTER":语法错误.

But I get the error: Near "ALTER": Syntax error.

有什么想法吗?

推荐答案

您不能使用 ALTER TABLE withcase.

您正在寻找获取表的列名::-

You are looking for getting the column names for a table::-

PRAGMA table_info(table-name);

PRAGMA

此编译指示为命名表中的每一列返回一行.结果集中的列包括列名、数据类型、是否该列是否可以为 NULL,以及该列的默认值.结果集中的pk"列对于不是主键的一部分,是主键中列的索引属于主键的列的键.

This pragma returns one row for each column in the named table. Columns in the result set include the column name, data type, whether or not the column can be NULL, and the default value for the column. The "pk" column in the result set is zero for columns that are not part of the primary key, and is the index of the column in the primary key for columns that are part of the primary key.

这篇关于检查 SQLite 中是否存在列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆