我是新手。我不认为我是唯一一个遇到这个问题的人,但我看到的所有其他问题似乎都没有涵盖这个特定问题。我只是想在 sqlite 中更新一个表,见下面的代码。此代码适用于 5.1 和 6.1 模拟器,但是当我在我的设备(iPhone 4 - iOS 6.1)上运行它时,“ sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);
”语句(从底部算起的第五个语句)使错误“if”语句(从底部算起的第四个语句)失败并给我“更新时出错。无法提交 - 没有事务处于活动状态”NLog 语句(底部的第三个语句)。如果有人能告诉我我做错了什么,我将非常感激。提前致谢。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
int rowCount = indexPath.row;
Game *game = [self.thegames objectAtIndex:rowCount];
cell.textLabel.text = game.GameDate;
NSString* str3=[game.GameDate substringToIndex:4];
NSString * Selected = [NSString stringWithFormat:@"%@%@", @"You Selected Game - ", str3];
displayLabel.text = Selected;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"sportretortdatab2.sqlite"];
NSString* str1 = displayLabel.text;
NSString *str2 = [str1 substringFromIndex: [str1 length] - 4];
sqlite3 *database;
sqlite3_stmt *updateStmt;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
{
NSString* sql= [NSString stringWithFormat:@"UPDATE ArchievedGameDate Set GameDate = \"%@\"", str2];
if(sqlite3_prepare(db, [sql cStringUsingEncoding:NSASCIIStringEncoding], -1, &updateStmt, NULL) != SQLITE_OK)
NSLog(@"Error while creating update statement. %s", sqlite3_errmsg(database));
}
char* errmsg;
sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);
if(SQLITE_DONE != sqlite3_step(updateStmt))
NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
sqlite3_finalize(updateStmt);
sqlite3_close(database);
}