How to Add a Boolean Column in Android SQlite

BST Kaal picture BST Kaal · Jul 7, 2014 · Viewed 62.3k times · Source

I have created a table for my ContentProvider using the following line :

static final String CREATE_DB_TABLE = 
  " CREATE TABLE " + CONTACTS_TABLE_NAME +
  " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
  " pid TEXT NOT NULL, " +
  " name TEXT NOT NULL,"+
  "number TEXT NOT NULL);";

It has 4 columns. Now i want to add a column with a boolean value of true/false. How can i add append/change this statement if i have to add a boolean column named "status".

Answer

Phantômaxx picture Phantômaxx · Jul 7, 2014

You could use something like this:

Create your table:

static final String CREATE_DB_TABLE = 
    "CREATE TABLE " + CONTACTS_TABLE_NAME " + 
    " (_id INTEGER PRIMARY KEY AUTOINCREMENT," + 
    "..." + " flag INTEGER DEFAULT 0)";

retrieve your value as:

Boolean flag = (cursor.getInt(cursor.getColumnIndex("flag")) == 1);