0

我有 sqlit 数据库我想在数据库中插入数据问题是当我插入数据时它在插入查询中显示以下错误。

- (void) addCoffee {
    if(addStmt == nil) {
        const char *sql = "insert into library(userID,contentAddedDateTime,contentType,contentTitle,contentSource,contentDescription,categoryTitle,subCategoryTitle,organizationCode,userName) Values(?,?,?,?,?,?,?,?,?,?)";
        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
    }

    sqlite3_bind_text(addStmt, 1, [userID UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 2, [contentAddedDateTime UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 4, [contentType UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 6, [contentTitle UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 7, [contentSource UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 8, [contentDescription UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 9, [categoryTitle UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 10, [subCategoryTitle UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 11, [organizationCode UTF8String], -1, SQLITE_TRANSIENT);

    if(SQLITE_DONE != sqlite3_step(addStmt))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
        //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
        coffeeID = sqlite3_last_insert_rowid(database);

    //Reset the add statement.
    sqlite3_reset(addStmt);
}

显示的错误是:

NSInternalInconsistencyException',原因:'创建添加语句时出错。'记不清''

4

1 回答 1

0

您需要在查询之前打开数据库。

使用sqlite3_open()打开你的数据库。

之前有这条线sqlite3_prepare_v2

if (sqlite3_open([databaseFilePath UTF8String], &database) == SQLITE_OK)

完成插入数据后,还可以使用sqlite3_finalize(statement);and 。sqlite3_close(database);

于 2013-07-27T06:36:33.910 回答