Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a column exists in SQLite

Tags:

sqlite

I need to check to see if a column exists and if it doesn't exist add it. From my research it looks like sqlite doesn't support IF statements and case statement should be used instead.

Here is what I have so far:

SELECT CASE WHEN exists(select * from qaqc.columns where Name = "arg" and Object_ID = Object_ID("QAQC_Tasks")) = 0 THEN ALTER TABLE QAQC_Tasks ADD arg INT DEFAULT(0); 

But I get the error: Near "ALTER": Syntax error.

Any ideas?

like image 415
Herrozerro Avatar asked Sep 20 '13 15:09

Herrozerro


People also ask

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.

How do I drop a column in SQLite?

DB Browser for SQLite allows you to add or drop columns. In the main view, tab Database Structure , click on the table name. A button Modify Table gets enabled, which opens a new window where you can select the column/field and remove it.

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 rename a column in SQLite?

It is not possible to rename a column, remove a column, or add or remove constraints from a table. The sqlite file format is very simple and that's why this operation is valid.

How to check that the SQLite database exists?

Checking that the SQLite database exists is easy, I can simply use code like: i.e. I can use the normal System.IO methods in .Net to check whether the database is present. Creating a SQLite database is also straightforward in SQLite. I can create the database file as follows:

How to check if a column exists in a SQLite tuple?

You can use sqlite's table_info operation to get a list of columns. From there, just use python to check if the column exists in the returned tuple. Here's a quick example:

Does SQLite support if statements?

I need to check to see if a column exists and if it doesn't exist add it. From my research it looks like sqlite doesn't support IF statements and case statement should be used instead.

How to test for the existence of a subquery in SQLite?

Summary: in this tutorial, you will learn how to use the SQLite EXISTS operator to test for the existence of rows returned by a subquery. The EXISTS operator is a logical operator that checks whether a subquery returns any row. In this syntax, the subquery is a SELECT statement that returns zero or more rows.


2 Answers

You cannot use ALTER TABLE withcase.

You are looking for getting the column names for a table::-

PRAGMA table_info(table-name); 

Check this tutorial on PRAGMA

This pragma returns one row for each column in the named table. Columns in the result set include the column name, data type, whether or not the column can be NULL, and the default value for the column. The "pk" column in the result set is zero for columns that are not part of the primary key, and is the index of the column in the primary key for columns that are part of the primary key.

like image 119
Rahul Tripathi Avatar answered Sep 21 '22 06:09

Rahul Tripathi


Althought this is an old question, I found at PRAGMA functions a simpler solution:

SELECT COUNT(*) AS CNTREC FROM pragma_table_info('tablename') WHERE name='column_name' 

If the result is more than zero then the column exists. Simple and one line query

The trick is to use

pragma_table_info('tablename') 

instead of

PRAGMA table_info(tablename) 

Edit: Please note that, as reported in PRAGMA functions:

This feature is experimental and is subject to change. Further documentation will become available if and when the table-valued functions for PRAGMAs feature becomes officially supported.

The table-valued functions for PRAGMA feature was added in SQLite version 3.16.0 (2017-01-02). Prior versions of SQLite cannot use this feature.

like image 20
Formentz Avatar answered Sep 21 '22 06:09

Formentz