Skip to content Skip to sidebar Skip to footer

Detect If A Table Contains A Column In Android/sqlite

So I have an app on the market, and with an update I want to add some columns to the database. No problems so far. But I want to detect if the database in use is missing these colu

Solution 1:

Pragma works. As an example I just tried it on one of my Android App DBs.

sqlite> pragma table_info (userCommand);

cid name        type    notnull     dflt_value  pk
0   id      INTEGER     011   description TEXT    002   message     TEXT    00

As we can see there are three fields in the specified table. id, description, and message.

So in your app, use something like:

Cursorc=null;
c = RawQuery ("pragma table_info (userCommand)",null); 
// if c has records and is not null then iterate through the records in search of your table name. 

Please excuse the poor formatting.

Solution 2:

This may be overkill, but you can select a single row from your table into a Cursor and use Cursor.getColumnIndex([column name as string]). This will return -1 if it doesn't exist.

Solution 3:

Try using PRAGMA with rawQuery() instead of query().

Solution 4:

You could issue the following command and go over the results and see if your column is listed.

pragma table_info(table_name);

Edit: I've never done this on Android, but I think it should work

Post a Comment for "Detect If A Table Contains A Column In Android/sqlite"