DISSENNATORE
DISSENNATORE

Reputation: 81

Working with boolean values in an SQLite database

I read that SQLite databases support a boolean value. Here's my query to create the table:

private static final String CREATE_TABLE_PROMEMORIE_RSI = "CREATE TABLE IF NOT EXIXSTS "
            + promemorieRSI.PROMEMORIE_RSI_TABLE + " ("
            + promemorieRSI.ID + " integer primary key autoincrement, "
            + promemorieRSI.GIORNO + " integer, "
            + promemorieRSI.MESE + " integer, "
            + promemorieRSI.ANNO + " integer, "
            + promemorieRSI.NOT_RIP_RSI + " boolean, "
            + promemorieRSI.RIP_VAL_RSI + " integer, "
            + promemorieRSI.UNIT_RIP_VAL_RSI + " text, "
            + promemorieRSI.NOT_FISSA_RSI + " boolean, "
            + promemorieRSI.ETICHETTA_RSI + " text, "
            + promemorieRSI.REQUEST_CODE_RSI + " integer);";

As you can see, I set the not_rip_rsi and not_fissa_rsi columns to be of boolean values. I read that the database stores the true and false values as, respectively, 1 and 0 integer values, but now I can't understand this:

For better explanation, here's my insert method:

public void inserisciPromemoriaRSI(Integer giorno, Integer mese, Integer anno, Boolean not_rip, Integer rip_val, String unit_rip_val, Boolean not_fissa, String etichetta, Integer request_code){
        ContentValues val = new ContentValues();
        val.put(promemorieRSI.GIORNO, giorno);
        val.put(promemorieRSI.MESE, mese);
        val.put(promemorieRSI.ANNO, anno);
        val.put(promemorieRSI.NOT_RIP_RSI, not_rip);
        val.put(promemorieRSI.RIP_VAL_RSI, rip_val);
        val.put(promemorieRSI.UNIT_RIP_VAL_RSI, unit_rip_val);
        val.put(promemorieRSI.NOT_FISSA_RSI, not_fissa);
        val.put(promemorieRSI.ETICHETTA_RSI, etichetta);
        val.put(promemorieRSI.REQUEST_CODE_RSI, request_code);
        mDb.insert(promemorieRSI.PROMEMORIE_RSI_TABLE, null, val);
    }

As you can see, to add the record I use a boolean value for these columns. Even if I set the value of the entries of those columns to be of type boolean, when I add a record, should I use an integer value of 1 or 0 instead of a boolean value, or will the database automatically store true as 1 and false as 0? Will I have to use a cursor.getInt(column_index) == 1 to get the boolean value? Am I making a mistake in my code? If so, how do I solve this problem. Any suggestions?

Upvotes: 2

Views: 8449

Answers (1)

Razor
Razor

Reputation: 1794

A boolean doesn't exists in SQLite. Instead, you store the boolean value in the database as an INTEGER in SQLite. The values that you can use are 0 for false and 1 for true. When you go and select the INTEGER from the database, to change it to a boolean, you use:

boolean isTrue = cursor.getInt(columnNo) > 0;

If you want a value of false, then:

boolean isFalse = cursor.getInt(columnNo) < 1;

Have a look at this post and this post for further information.

Upvotes: 2

Related Questions