Android – how do I set the default value for the text column in SQLite?
I have a simple table. I'm trying to put the default value into the text column. This is a table query:
"CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT, book_id TEXT NOT NULL DEFAULT '0', book_name TEXT NOT NULL);"
It creates a table, but there's a problem when I try to insert data. I just try to give book_ Name is a book title because I expect book_ The default value of ID column is 0. But it does not. It adds a null value, so the row will not be inserted. I also tried this query:
"CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT, book_id TEXT NOT NULL DEFAULT \'0\', book_name TEXT NOT NULL);"
But the problem still exists. I have searched for stack overflow and got some answers, but they are old and not suitable for me now. Therefore, how to set the default value to text column in SQLite has changed. Thanks in advance:)
Edit this is the insert statement:
database.insert("book", null, cv);
Here CV is the object of contentvalues, which contains only the column book_ Value of name
resolvent:
You can specify default values for columns when creating a table. (it looks like you can't use the alter statement to add default values, so you have to recreate your table.)
CREATE TABLE your_table_name
(MainContactName TEXT NOT NULL DEFAULT '')
For example,
CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT,book TEXT DEFAULT "abc");
Now you see that the default value is set to "ABC"
Check sqllite documentation