I am trying to figure how to insert a null value into an SQLite table in Android.
This is the table:
"create table my table (_id integer primary key autoincrement, " + "deviceAddress text not null unique, " + "lookUpKey text , " + "deviceName text , " + "contactName text , " + "playerName text , " + "playerPhoto blob " + ");";
I wanted to use a simple Insert command via execSQL
but since one of the values is a blob I can't do it (I think).
So, I am using a standard db.Insert
command. How do I make one of the values null?
If I just skip it in the ContentValues
object will it automatically put a null value in the column?
You also can specify the NULL keyword in the VALUES clause to indicate that a column should be assigned a NULL value. The following example inserts values into three columns of the orders table: INSERT INTO orders (orders_num, order_date, customer_num) VALUES (0, NULL, 123);
You can insert NULL value into an int column with a condition i.e. the column must not have NOT NULL constraints. The syntax is as follows. INSERT INTO yourTableName(yourColumnName) values(NULL); To understand the above syntax, let us first create a table.
Based on the SQL standard, PRIMARY KEY should always imply NOT NULL . However, SQLite allows NULL values in the PRIMARY KEY column except that a column is INTEGER PRIMARY KEY column or the table is a WITHOUT ROWID table or the column is defined as a NOT NULL column.
You can use ContentValues.putNull(String key) method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With