Matheus Valin
Matheus Valin

Reputation: 189

Table column not updated after Alter Table in SQLite

I want to add a column to a table in case the column doesn't exist,but SQLite doesn't seem to update the column list in Android

currently i'm using:

DB.execSQL("alter table "+table_name+" add column "+column_name+" text");
DB.rawQuery("select * from "+table_name+" limit 1",null).getColumnNames();

but the string list returned from "getColumnNames" does not contain the column I just created, so when I check again for the same column, it understands the column doesn't exist and tries to create it again, which causes a "duplicated column" exception

Thanks in advance for any help,this is my first question in SO :)

Upvotes: 2

Views: 852

Answers (2)

Pasquale Piccolo
Pasquale Piccolo

Reputation: 311

Try to discard the result of the first query after the "alter table" command and use the result of a second query. It worked for me.

Upvotes: 0

Francesc
Francesc

Reputation: 29280

You need to increase the database number, which will call onUpgrade on your next app launch. In your onUpgrade method, you run the SQLite command to alter the table.

See here for some examples.

Upvotes: 1

Related Questions