我是新手。我不认为我是这个问题中唯一的一个,但是我所看到的所有其他问题似乎都没有涉及到这个问题。我只是想在sqlite中更新一个表,见下面的代码。此代码适用于5.1和6.1模拟器,但是当我在我的设备(iPhone 4 - iOS 6.1)上运行它时,“sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);
”语句(底部的第五条语句)未能通过错误“if”语句底部,并给我“更新时出错,无法提交 - 没有事务处于活动状态”NLog语句(底部的第三条语句)如果有人可以告诉我我做错了什么,我会非常感激。“sqlite3_exec(database,”COMMIT“,NULL,NULL,&errmsg)”语句在5.1和6.1模拟器上工作,但不在iPhone 4设备上
- (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);
}
感谢您花时间查看我的问题。但是,你的建议并不适合我。我注意到,当我提出您的建议更改时,我试图存档。那失败了。它的档案也没有提出你的建议更改失败。我正在研究为什么会发生这种情况。有任何想法吗?再次感谢您的时间。 – 2013-02-17 16:21:41
在sqlite3_prepare中,您应该尝试给出SQL语句的长度不是-1。也许它错过了零终结者。 – DebugMedve 2013-02-17 17:35:19
感谢您的建议,但这也没有奏效。 – 2013-02-18 04:38:24