ALTER TABLE Sqlite: how to check if a column exists before alter the table?
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
IMO this
conn = sqlite3.connect(':memory:')
c = conn.cursor()
try:
c.execute('ALTER TABLE mytable ADD COLUMN newcolumn;')
except:
pass # handle the error
c.close()
is a better choice than constructing special case queries.
You can wrap the above code in a AddColumn(cursor, table, column) function so you can reuse it,
plus it'll make the code more readable.