50

I had a table modified to add status column to it in this fashion

ALTER TABLE ITEM ADD COLUMN STATUS VARCHAR DEFAULT 'N';

However SQLite doesnt seem to add N to the column for any new ITEM created. Is the syntax wrong or is there any issue with SQLite and its support for defaults.

I am using SQLite 3.6.22

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Azlam
  • 2,052
  • 3
  • 24
  • 28
  • 4
    What version of sqlite are you using? I am using 3.6.16 and the sql you provided worked perfectly for me. – Jansen Price Feb 12 '10 at 19:27
  • 2
    Maybe it is a problem with a frontend you are using to manipulate the db. Have you tried the non-functional `insert` to execute "by hand" (in sqlite cli client)? – Messa Feb 12 '10 at 19:34
  • 2
    It seems it was actually working , the problem was with frontend using to manipulate the DB – Azlam Feb 12 '10 at 19:45

1 Answers1

61

Looks good to me. Here are the Docs.

sqlite> create table t1 (id INTEGER PRIMARY KEY, name TEXT, created DATE);
sqlite> .table
t1
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1 (id INTEGER PRIMARY KEY, name TEXT, created DATE);
COMMIT;

sqlite> alter table t1 add column status varchar default 'N';
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1 (id INTEGER PRIMARY KEY, name TEXT, created DATE, status varchar default 'N');
COMMIT;

sqlite> insert into t1 (name) values ("test");
sqlite> select * from t1;
1|test||N

Dump your schema and verify that your table structure is there after calling ALTER TABLE but before the INSERT. If it's in a transaction, make sure to COMMIT the transaction before the insert.

$ sqlite3 test.db ".dump"
databyte
  • 1,228
  • 10
  • 7