Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite If Column Exists

Tags:

I was wondering if there is a nice IF NOT EXISTS for checking columns and indexes in SQLite, or do I need to bring back the entire database schema and validate against that?

like image 699
Nathan Avatar asked Mar 26 '10 03:03

Nathan


People also ask

How do you check if a table has a column SQLite?

SELECT INSTR(sql, '<column_name>') FROM sqlite_master WHERE type='table' AND name='<table_name>'; Returns 0 (zero) if column <column_name> does not exists in the table <table_name> .

Does SQLite support procedures?

Stored procedures aren't really needed for Sqlite, since the program you write around Sqlite can certainly implement any 'stored' procedures itself. But beyond that you can certainly implement user functions which provide stored procedure results; but that's not a thing other databases can do.

How do I add multiple columns in SQLite?

SQLite does not support adding multiple columns to a table using a single statement. To add multiple columns to a table, you must execute multiple ALTER TABLE ADD COLUMN statements.

How do I add a column to a table in SQLite?

Syntax. The syntax to ADD A COLUMN in a table in SQLite (using the ALTER TABLE statement) is: ALTER TABLE table_name ADD new_column_name column_definition; table_name.


1 Answers

There is a system catalog table called sqlite_master that you can use to check index (or other) names:

SELECT name FROM sqlite_master WHERE type='index' ORDER BY name; 

You can use a pragma to get the indexed columns:

PRAGMA index_info(index-name); 

And this one to get the column names for a table:

PRAGMA table_info(table-name); 
like image 101
Tim Avatar answered Sep 20 '22 03:09

Tim