ALTER TABLE Sqlite:在更改表之前如何检查一列是否存在? [英] ALTER TABLE Sqlite: how to check if a column exists before alter the table?

查看:127
本文介绍了ALTER TABLE Sqlite:在更改表之前如何检查一列是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在sql中执行一个SQL查询,该查询在sqlite3中添加了一个新列.

I need to execute in python a SQL query that adds a new column, in sqlite3.

问题是有时它已经存在.因此,在执行查询之前,我需要检查该列是否已经存在.

The problem is that sometimes it already exists. So previous to executing the query I need to check if the column already exists.

如果这样做,那么我将不会执行查询.

If it does, then I won't execute the query.

在sqlite中是否可以做到这一点?还是我必须通过python代码中的try-catch块来实现它?

非常感谢!

推荐答案

您可以通过以下语句获取表的列列表:

You can get a list of columns for a table via the following statement:

PRAGMA table_info('table_name');

有关pragma命令的更多详细信息,请参见 sqlite网站

More details on the pragma commands are availabel at the sqlite web site

这篇关于ALTER TABLE Sqlite:在更改表之前如何检查一列是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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