2010-06-14 49 views
0

我似乎误解了该基本语法,为什么这个样品的工作原理:SQLITE基本语法

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)); 

但当我尝试此查询: “插入测试(strtest)VALUES(‘嘘’);”

我收到一个错误: 错误:sqlite3_step()19.错误消息约束失败;

我错过了什么?

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

感谢, Doori酒吧

回答

4

所有三列被声明为NOT NULL,而他们没有一个默认值。

所以您的查询:

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

只分配blobtest值,留出strtestinttest不能为空。

您可以通过增加两个数值解决这个问题,或者您可以更改表模式为默认的东西,如:

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

(或者你可以拿出NOT NULL如果你想他们是NULL -able)

+0

非常感谢! (我想你实际上意味着strtest被设置为'boo',但blobtest和inttest没有NULL或缺省值,对吗? – 2010-06-14 15:41:49

+0

@Doori Bar - Nope,'blobtest'是你的模式中声明的第一列,所以除非你指定明确的列顺序,例如:'insert into test(inttest,strtest,blobtest)values(...)' – Matt 2010-06-14 15:43:56

+0

但是我做了什么? “insert into test(strtest)values('boo');” – 2010-06-14 15:50:21