Monty
Monty

Reputation: 3215

sqlite with boolean values?

i am getting some text and boolean values from server i need to save them in database.

this is my table . I defined boolean values as INTEGER couse in sqlite there is no boolean.

db.execSQL("CREATE TABLE outcomesStore(id INTEGER PRIMARY KEY AUTOINCREMENT , allowgo INTEGER,cod TEXT,youdidComments INTEGER, youwent INTEGER,ByDate INTEGER ," +
                "OnCompletion INTEGER,yourtext TEXT , yourGroup TEXT, yourConsultation INTEGER )");

and i am getitng these values from server.

Store[] Storedata = Configuration.getStore();


    booleanvalues[0] = Store[0].isallowgo ();

and inserting like this

 helperdatabase = new DatabaseHelperInurseBusiness(this);
            db = helperdatabase.getWritableDatabase();

            ContentValues insertOutcomes = new ContentValues();


            insertOutcomes.put(helperdatabase.ALLOW_GO,booleanvalues[0]);
        db.insert("outcomesStore", helperdatabase.ALLOW_GO,insertOutcomes);

Its not working even not giving any error.

Upvotes: 2

Views: 24779

Answers (2)

Monty
Monty

Reputation: 3215

i got the solution.

Thanks Yaqub Ahamad.

    insertOutcomes.put(DatabaseHelperInurseBusiness.ALLOW_GO,storedata.isAllowGo()== true ? 1:0);

Upvotes: -2

mvp
mvp

Reputation: 116477

Actually, SQLite does support BOOLEAN type, but may be not exactly in the way you expect.

You can create column of BOOLEAN type using standard CREATE TABLE, and then populate it:

CREATE TABLE mytable (name VARCHAR(10), flag BOOLEAN);
INSERT INTO mytable (name, flag) VALUES ('Alice', 0);
INSERT INTO mytable (name, flag) VALUES ('Bob',   1);

Then you can get your data back, and use standard BOOLEAN logic while doing so:

SELECT * FROM mytable WHERE flag

or using different BOOLEAN expressions:

SELECT * FROM mytable WHERE NOT flag

and so on. (Obligatory SQLFiddle)

In other words, it all works great, the only catch is that you must use 0 instead of FALSE and 1 instead of TRUE (this includes trying to set values from client software). Note that this is somewhat similar to other SQL engines (For example, PostgreSQL supports using '0'/'1', 'f'/'t' and false/true for setting FALSE/TRUE values by client software).

Also, if you were to use this BOOLEAN field in numeric context (like adding or multiplying) it will behave as number 0 or 1, while in other SQL engines adding BOOLEAN and INTEGER may cause an exception because of incompatible types.

Upvotes: 27

Related Questions