tags:

views:

44

answers:

1

I seem to misunderstand a basic syntax, why this sample works:

sqlite3_prepare_v2(db, "insert into test values('boo','boo',0);", strlen(querystring)+1 , &stmt, NULL);

if ((rc = sqlite3_step(stmt)) != SQLITE_DONE)
  fprintf(stderr, "Error: sqlite3_step() %d. Error Message %s;\n",rc,sqlite3_errmsg(db));

But when I try this query: "insert into test(strtest) values('boo');"

I get an error: Error: sqlite3_step() 19. Error Message constraint failed;

What am I missing?

table test is: "create table test (blobtest BLOB(4) NOT NULL, strtest VARCHAR NOT NULL, inttest INTEGER NOT NULL );"

Thanks, Doori Bar

+4  A: 

All three columns are declared as NOT NULL, and they do not have a default value.

So your query:

insert into test(strtest) values('boo');

Is only assigning blobtest a value, leaving out strtest and inttest which cannot be null.

You can fix this by either adding two more values, or you can change the table schema to default to something, e.g.:

create table test (
    blobtest BLOB(4) NOT NULL,
    strtest VARCHAR NOT NULL DEFAULT '',
    inttest INTEGER NOT NULL DEFAULT 0
);

(or you could take out NOT NULL if you would like them to be NULL-able)

Matt
Thank you very much! (I suppose you actually meant that strtest being set to 'boo' but blobtest and inttest been left with no NULL or a default, right?
Doori Bar
@Doori Bar - Nope, `blobtest` is the first column declared in your schema, so the first value in `values(...)` will be applied to that column, unless you specify explicit column ordering, e.g.: `insert into test (inttest, strtest, blobtest) values(...)`
Matt
@Matt: But I did? "insert into test(strtest) values('boo');"
Doori Bar
Oh yea, you did. Sorry, I over-read (twice) :)
Matt
@Matt: thanks a lot mate :)
Doori Bar