Do not add to the database when any value is null

0

How do I not add to the database when the String day is null?

Code:

      public boolean insertData(String disciplina,String sala,String dia,String hora){
    ContentValues values = new ContentValues();
    values.put(COL_2,disciplina);
    values.put(COL_3,sala);
    values.put(COL_4,dia);
    values.put(COL_5,hora);
    long result = database.insert(TABLE_NAME,null,values);
        if(result==-1){
            return false;
        }else
            return true;


}
    
asked by anonymous 12.07.2017 / 23:02

1 answer

2

You have 2 options:

1) Set the creation of the table that the column can not receive null and void if you try to enter the bank puts a default value. Ex:

CREATE TABLE tabela (coluna TEXT NOT NULL DEFAULT 'texto qualquer');

2) Set to create the table that the column can not receive zero and method that inserts must check before and abort if the value is zero. Ex:

CREATE TABLE tabela (coluna TEXT NOT NULL);

In method:

public boolean insertData(String valor_coluna){
    if(valor_coluna == null || TextUtils.isEmpty(valor_coluna)) return false;

    ContentValues values = new ContentValues();
    values.put(coluna, valor_coluna);

    return database.insert(TABLE_NAME,null,values) != -1;
}
    
12.07.2017 / 23:53