How to Add a Boolean Column in Android SQlite How to Add a Boolean Column in Android SQlite sqlite sqlite

How to Add a Boolean Column in Android SQlite


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);


As mentioned by M D,

SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).

Use the following ALTER statement -

ALTER TABLE CREATE_DB_TABLE ADD status boolean NOT NULL default 0;

Then you can use Cursor to get the required value and then convert to actual boolean -

flag=cursor.getString(0).equals("1")

You can use this flag to display on user screen.

Reference: http://www.sqlite.org/datatype3.html


public enum Status{        TRUE,FALSE;}static final String CREATE_DB_TABLE =   " CREATE TABLE " + CONTACTS_TABLE_NAME +  " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +......  " booleanColumn INTEGER DEFAULT 0);";

//while inserting

pass Status.TRUE.ordinal() values to database and

//while retrieving

cross check with the ordinal value for status