7

How can I add a column in an SQLite table if and only if the same column does not exist in the table?

Using ALTER TABLE I am able to create a new column but want to know how to check whether that column already exists in the table or not?

5
  • 1
    You can view the table columns by using '.schema tableName' Commented Jan 8, 2011 at 11:34
  • 1
    @krakover Isn't that an answer? Commented Jan 8, 2011 at 11:42
  • @goreSplatter Depending on what he is trying to achieve. I explained how to avoid the problem, not how to solve it..\ Commented Jan 8, 2011 at 11:47
  • @krakover I see no problem to be avoided, but a question how to determine the presence of a column. Which in turn can be determined using your comment. But that's probably OT. Commented Jan 8, 2011 at 11:51
  • 1
    possible duplicate of ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite Commented Jan 8, 2011 at 17:59

4 Answers 4

8

SQLite returns an error like "no such column: foo" if the table doesn't contain the column:

  select foo from yourTable limit 1

Also you can get the create-table statement:

 select sql from sqlite_master where tbl_name = 'YourTableName'

and then parse the result, looking for the column-name. I don't know of an elegant way to query the list of columns for a specified table, though one may exist.

Also if you attempt to do this:

alter table YourTable add column foo {column-def whatever it is}

you get an error from SQLite if the column already exists. You could trap that error too.

Finally you could do this:

  select sql from sqlite_master 
  where tbl_name = 'YOURTABLE' and sql like '%"foo" CHAR%';    -- or whatever type

and if the specified table contains the column which is surrounded by double-quotes in the query, and with the type you have specified, you will get a result, otherwise an empty set. Specifying the datatype ensures that your LIKE substring match occurs on a column-name.

Sign up to request clarification or add additional context in comments.

Comments

5

There's no way (that I know of) to do it all in a single SQLite query. You must use application code to manage the If/Elseness.

Check if table exists or not:

select count(*) from sqlite_master where type = 'table' and name = MyTable';

Check if column exists in table or now

pragma table_info(thumbnail);

However, a better approach may be explicit database schema updates based on schema versions your application maintains (e.g. specific alter table statement to go from schema version 1 to 2):

pragma user_version;

Comments

1

It seems like that it is impossible to do checking if the column not exists and addindg the new column in one command, because Sqlite don't support "IF NOT EXISTS" for column. "IF NOT EXISTS" works only on table. Here is what I will do:

rev = ExecuteStatement("SELECT columnNamexx FROM tableNamexx limit 1;");

if(rev != SQLITE_OK){ // add col to table
    ExecuteStatement("ALTER TABLE tableNamexx ADD COLUMN columnNamexx INTEGER DEFAULT 0;");
}

Comments

0

You can view the table columns by using '.schema tableName'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.