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