2016-02-25 48 views
2

我正在使用sqlite插入和更新表視圖中的數據。我只能在數據庫鎖定的下一次嘗試中更新數據。即使關閉數據庫,請幫助。以下是代碼。Sqlite數據庫鎖定錯誤 - iOS

-(void)saveUserCredential: (NSString *)email :(NSString *)userName :(NSString *)loginTime :(NSString *)source 
{ 
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init]; 
[dateformate setDateFormat:@"yyyy-MM-dd HH:mm"]; // Date formater 
NSString *todayDate = [dateformate stringFromDate:[NSDate date]]; 

const char *dbpath = [databasePath UTF8String]; 
if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
{ 
    NSString * query = @"SELECT * from users"; 
    int rc =sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL); 
    if(rc == SQLITE_OK) 
    { 
     if(sqlite3_step(statement) == SQLITE_ROW) 
     { 

       NSString *updateSQL = [NSString stringWithFormat:@"update users set email = '%@', username = '%@', source = '%@', created = '%@' where id = '%d'",email, userName, source, todayDate,1]; 

      const char *update_stmt = [updateSQL UTF8String]; 
      sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL); 
      //sqlite3_bind_int(statement, 1, 1); 
      if (sqlite3_step(statement) == SQLITE_DONE) 
      { 
       NSLog(@"successfully updated"); 
      } 
      else{ 

       NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database)); 
      } 

     } 
     else 
     { 
      NSString *insertSQL = [NSString stringWithFormat:@"insert into users (email,username,source,created) values('%@','%@','%@','%@')",email,userName,source,todayDate]; 

      NSLog(@"INS SQL: %@", insertSQL); 
      const char *insert_stmt = [insertSQL UTF8String]; 
      sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL); 
      if (sqlite3_step(statement) == SQLITE_DONE) 
      { 
       NSLog(@"INSERTED"); 
      } 
      else 
      { 
       NSLog(@"NOT INSERTED"); 
      } 
      NSLog(@"hello "); 

     } 
     sqlite3_finalize(statement); 
     sqlite3_close(database); 

    } 

} 

} 

回答

2

你需要調用

sqlite3_finalize(statement); 

每次成功sqlite_prepare_v2通話。它應該是平衡的。還爲每個查詢使用不同的sqlite3_stmt

所以需要你這樣的代碼進行更改:

const char *dbpath = [databasePath UTF8String]; 
if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
{ 
    NSString * query = @"SELECT * from users"; 
    int rc =sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL); 
    if(rc == SQLITE_OK) 
    { 
     if(sqlite3_step(statement) == SQLITE_ROW) 
     { 

       NSString *updateSQL = [NSString stringWithFormat:@"update users set email = '%@', username = '%@', source = '%@', created = '%@' where id = '%d'",email, userName, source, todayDate,1]; 

      const char *update_stmt = [updateSQL UTF8String]; 
      sqlite3_stmt *upStmt; 
      sqlite3_prepare_v2(database, update_stmt, -1, &upStmt, NULL); 
      //sqlite3_bind_int(upStmt, 1, 1); 
      if (sqlite3_step(upStmt) == SQLITE_DONE) 
      { 
       NSLog(@"successfully updated"); 
      } 
      else 
      { 

       NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database)); 
      } 
      sqlite3_finalize(upStmt); 
     } 
     else 
     { 
      NSString *insertSQL = [NSString stringWithFormat:@"insert into users (email,username,source,created) values('%@','%@','%@','%@')",email,userName,source,todayDate]; 

      NSLog(@"INS SQL: %@", insertSQL); 
      const char *insert_stmt = [insertSQL UTF8String]; 
      sqlite3_stmt *inStmt; 
      sqlite3_prepare_v2(database, insert_stmt,-1, &inStmt, NULL); 
      if (sqlite3_step(inStmt) == SQLITE_DONE) 
      { 
       NSLog(@"INSERTED"); 
      } 
      else 
      { 
       NSLog(@"NOT INSERTED"); 
      } 
      NSLog(@"hello "); 
      sqlite3_finalize(inStmt); 

     } 
     sqlite3_finalize(statement); 
     sqlite3_close(database); 

    } 

} 
+0

所以我需要這行之後添加的finalize語句if(sqlite3_step(聲明)== SQLITE_DONE) { 的NSLog(@ 「成功更新」) ; (@「error while update。'%s'」,sqlite3_errmsg(database)); } –

+0

@Mac_Play:我已經用代碼 –

+0

更新了我的回答。它現在不更新表格。 –