2013-02-17 66 views
0

我是新手。我不认为我是这个问题中唯一的一个,但是我所看到的所有其他问题似乎都没有涉及到这个问题。我只是想在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); 
    } 

回答

0

我会尝试:

if(sqlite3_step(updateStmt) != SQLITE_DONE) 

int step = sqlite3_step(updateStmt); 
if(step != SQLITE_DONE) 

,无:

sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg); 

,这真的有必要在这方面?你开始交易了吗?我猜sqlite是在自动提交模式下,但你可以检查sqlite3_get_autocommit()接口。如果给定的数据库连接分别处于或不处于自动提交模式,它将返回非零或零。自动提交模式默认开启。自动提交模式由BEGIN语句禁用。通过COMMIT或ROLLBACK重新启用自动提交模式。

,并把所有的SQLite功能到语句:

if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) 

,我也希望“DB”在此准备第一个参数是“数据库”,在活动代码,因为这意味着你的工作在两个数据库实例。

sqlite3_prepare(db, [sql cStringUsingEncoding:NSASCIIStringEncoding], -1, &updateStmt, NULL) != SQLITE_OK) 

我希望它能解决您的问题。

+0

感谢您花时间查看我的问题。但是,你的建议并不适合我。我注意到,当我提出您的建议更改时,我试图存档。那失败了。它的档案也没有提出你的建议更改失败。我正在研究为什么会发生这种情况。有任何想法吗?再次感谢您的时间。 – 2013-02-17 16:21:41

+0

在sqlite3_prepare中,您应该尝试给出SQL语句的长度不是-1。也许它错过了零终结者。 – DebugMedve 2013-02-17 17:35:19

+0

感谢您的建议,但这也没有奏效。 – 2013-02-18 04:38:24

相关问题