2013-08-07 22 views
0
在SQLite數據庫行

我打算從一個自定義對象在我的SQLite數據庫中插入一個方法:不插入iOS中

- (void)insertCustomEntity:(CustomEntity *)customEntity 
{ 
    NSString *filePath = [FileMngr copyDatabaseToDocuments]; 

    sqlite3 *database; 

    if (sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) { 
     const char *sqlStatement = "INSERT OR REPLACE INTO Entities (id, type, timestamp, result) VALUES (?, ?, ?, ?)"; 
    sqlite3_stmt *compiledStatement; 

     NSLog(@"Could not prepare statement: %s\n", sqlite3_errmsg(database)); 

     if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 
      sqlite3_bind_int(compiledStatement, 1, customEntity.id); 
      sqlite3_bind_int(compiledStatement, 2, customEntity.type); 
      sqlite3_bind_int64(compiledStatement, 3, customEntity.timestamp); 
      sqlite3_bind_int(compiledStatement, 4, customEntity.result); 
     } 

      if(sqlite3_step(compiledStatement) == SQLITE_DONE) { 

      } 
      else { 
      NSLog(@"Step: %d\n", sqlite3_step(compiledStatement)); 
      } 
     } 

     sqlite3_finalize(compiledStatement); 
    } 

    sqlite3_close(database); 
} 

我已經做了一段時間,數據測試中被插入,但突然停止了,我現在正在第二個NSLog獲得5步代碼。爲什麼會發生這種情況,如果它以前工作?我嘗試了幾個選項,例如在sqlite3_prepare_v2中放置sqlite3_step(compiledStatement),但我一直無法插入。我做錯了什麼?

在此先感謝

+0

是U投入的NSLog(@ 「%S」,sqlite3_errmsg(數據庫))後,歌廳任何類型的錯誤消息;並在其他部分放入相同的語句並檢查錯誤消息 –

回答

1

我不知道,但它看起來像你的一些括號中的不匹配?無論如何,錯誤代碼5表示您的數據庫已被鎖定。

http://www.sqlite.org/c3ref/c_abort.html

檢查別的東西訪問數據庫(或你的數據庫是不是在某些情況下在這種情況下,你的代碼需要改變越來越關閉)。請嘗試以下解決方案來解決它: -

sqlite3 database is getting locked