Search code examples
sqliteandroid-sqlite

How do I insert a Boolean Value into my SQLite Database?


This is my function to add Data:

public boolean addData(String item1, boolean done) throws SQLException {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(COL2, item1);
        cv.put(COL3, done);

        long result = db.insert(TABLE_NAME, null, cv );

        //if date was inserted incorrectly it will return -1, thus returning false here
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }

And how I create my Database plus the column and table names:

public static final String DATABASE_NAME = "mylist.db";
public static final String TABLE_NAME = "mylist_data";
public static final String COL1 = "id";
public static final String COL2 = "name";
public static final String COL3 = "isdone";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME +  " 
 ("+ COL1 +" INTEGER PRIMARY KEY AUTOINCREMENT, " 
   + COL2 + " TEXT,"
   + COL3 +" BOOLEAN )";
    db.execSQL(createTable);
}

When executed, the insert statement returns "-1", meaning it did not complete the insertion. Any idea how to save a boolean value into a Database?

(The issue does reside with the boolean, since when commented out of code, i can insert values without problem).


Solution

  • Look here: https://www.sqlite.org/datatype3.html in 2.1

    use an integer with values 0 and 1 instead.

    You are using C# or Java? Please tag it accordingly.

    And Set COL3 in the table creation sql to INTEGER and change line "cv.put(COL3, done);" to "cv.put(COL3, done? 1: 0);":

    public boolean addData(String item1, boolean done) throws SQLException {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(COL2, item1);
            cv.put(COL3, done? 1: 0);
    
            long result = db.insert(TABLE_NAME, null, cv );
    
            return (result != -1); 
        }
    

    I removed comment of -1, because it is documented in reference https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#insert(java.lang.String,%20java.lang.String,%20android.content.ContentValues) therefore comment is void.

    This is a question in C# - I thought first - but now in Java:

    public static final String DATABASE_NAME = "mylist.db";
    public static final String TABLE_NAME = "mylist_data";
    public static final String COL1 = "id";
    public static final String COL2 = "name";
    public static final String COL3 = "isdone";
    
    
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME +  " 
     ("+ COL1 +" INTEGER PRIMARY KEY AUTOINCREMENT, " 
       + COL2 + " TEXT,"
       + COL3 +" INTEGER)";
        db.execSQL(createTable);
    }