此函数中的所有内容似乎都按预期工作,只是没有记录实际显示在数据库表中。如果我直接通过 Firefox sqlite manager 插件输入 sql,记录显示就好了。可能是一些令人沮丧的愚蠢,有什么想法吗?
- (IBAction)buttonInsertRecord:(id)sender
{
// initializing db file access
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"KJV.sqlite"];
// testing to see if database was found
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
// database not found
labelStatus.text = @"database not found";
}
else
{
// database not found
labelStatus.text = @"database found";
}
sqlite3_stmt *statement;
sqlite3 *dbPointer;
if (sqlite3_open(dbPath.UTF8String, &dbPointer) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO users (user_name, user_id) VALUES ('testuser', null);"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(dbPointer, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
labelStatus.text = @"added";
}
else
{
labelStatus.text = @"failed";
}
sqlite3_finalize(statement);
sqlite3_close(dbPointer);
}
}