我有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',reason:'创建add语句时出错。 '失去记忆''
答案 0 :(得分:0)
您需要在查询之前打开数据库。
使用sqlite3_open()打开您的数据库。
在sqlite3_prepare_v2
之前使用此行。
if (sqlite3_open([databaseFilePath UTF8String], &database) == SQLITE_OK)
插入数据后,也可以使用sqlite3_finalize(statement);
和sqlite3_close(database);
。