ALTER TABLE Sqlite: how to check if a column exists before alter the table?

Esabe picture Esabe · Mar 1, 2010 · Viewed 16.5k times · Source

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.

Is there a way in sqlite to do that? Or do I have to make it through a try-catch block in python code?

Thanks a lot in advance!

Answer

My Other Me picture My Other Me · Mar 1, 2010

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

PRAGMA table_info('table_name');

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