我有一个方法旨在从自定义对象插入到我的 sqlite 数据库中:
- (void)insertCustomEntity:(CustomEntity *)customEntity
{
NSString *filePath = [FileMngr copyDatabaseToDocuments];
sqlite3 *database;
if (sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "INSERT OR REPLACE INTO Entities (id, type, timestamp, result) VALUES (?, ?, ?, ?)";
sqlite3_stmt *compiledStatement;
NSLog(@"Could not prepare statement: %s\n", sqlite3_errmsg(database));
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_int(compiledStatement, 1, customEntity.id);
sqlite3_bind_int(compiledStatement, 2, customEntity.type);
sqlite3_bind_int64(compiledStatement, 3, customEntity.timestamp);
sqlite3_bind_int(compiledStatement, 4, customEntity.result);
}
if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
}
else {
NSLog(@"Step: %d\n", sqlite3_step(compiledStatement));
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
我已经进行了一段时间的测试并且正在插入数据,但突然停止了,我现在在第二个 NSLog
处获得了 5 步代码。如果之前可以正常工作,为什么会发生这种情况?我尝试了在一些帖子中找到的几个选项,例如将 sqlite3_step(compiledStatement)
放入 sqlite3_prepare_v2
中,但我始终无法插入。我做错了什么?
提前致谢
最佳答案
我不确定,但看起来你的一些牙套不匹配?不管怎样,错误代码 5 表明您的数据库已被锁定。
http://www.sqlite.org/c3ref/c_abort.html
检查是否有其他东西正在访问您的数据库(或者在某些情况下您的数据库没有关闭,在这种情况下您的代码需要更改)。尝试以下解决方案来解决它:-
关于ios - 不在 iOS 中的 sqlite 数据库中插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18102275/