SQLite3插入错误C ++

时间:2018-07-17 00:00:19

标签: c++ sqlite

我要插入sqlite3表公司,我在其中使用以下架构创建表

sql = "CREATE TABLE COMPANY("  \
         "NAME TEXT PRIMARY KEY     NOT NULL,"
         "ADDRESS           TEXT    NOT NULL;";

我能够创建表,但是当我使用以下命令通过sqlite3_bind_text插入时,出现以下问题

const char* sql = "INSERT INTO COMPANY(NAME, ADDRESS) VALUES (?, ?)"
sqlite3_stmt *stmt;
int rc = sqlite3_exec(db, sql, callback, &stmt, &zErrMsg);
if(rc == SQLITE_OK ) {
    sqlite3_bind_text(stmt, 1, "abc", 3, 0);
    sqlite3_bind_text(stmt, 2, "def", 3, 0);
    // commit
    sqlite3_step(stmt) ;
    sqlite3_finalize(stmt) ;
}

我遇到类似的错误,     SQL错误:COMPANY.NAME不能为NULL

当column不为null时,sqlite3绑定文本不起作用吗?

1 个答案:

答案 0 :(得分:0)

const char* sql = "INSERT INTO COMPANY(NAME, ADDRESS) VALUES (?, ?)"
sqlite3_stmt *stmt;
char *pszTest ;
int rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &pszTest) ;
if(rc == SQLITE_OK ) {
    sqlite3_bind_text(stmt, 1, "abc", 3, 0);
    sqlite3_bind_text(stmt, 2, "def", 3, 0);
    // commit
    sqlite3_step(stmt) ;
    sqlite3_finalize(stmt) ;
}