-2

I am new to SQLite. I want to add a column if it does not exist.

How to check if the column name exists and then add if it does not?

I tried

ALTER TABLE table ADD COLUMN colname INTEGER ON CONFLICT IGNORE

But it shows an error

Result: near "ON": syntax error

Any advice how it can be achieved?

Thanks in advance.

Alexey
  • 2,439
  • 1
  • 11
  • 15
Rahul
  • 1
  • 3
  • does this answer your question? https://stackoverflow.com/questions/24571611/mysql-alter-table-if-column-not-exists – Jocohan Jul 15 '22 at 04:03

1 Answers1

-1

First get a list of table column names - as list - with something like:

select group_concat(c.name) from pragma_table_info('table_name') c;

Then do a CASE expression on whether the new column name you want to add exists in the list above. More info at: https://www.sqlite.org/lang_expr.html

Barry the Platipus
  • 9,594
  • 2
  • 6
  • 30
  • 1
    I used "PRAGMA table_info($_tableName);" which gave all column names and extracted the column which i needed from the map to know if it exists. – Rahul Jul 15 '22 at 11:46