我在ios下面写sqlite3怎么都不行啊,一直都是返回SQLITE_BUSY。
刚开始我是因为没有把数据库文件copy到document路径下面,copy到document下之后,还是不能写。
- (BOOL) initializeDb
{
NSLog (@"initializeDB");
// look to see if DB is in known location (~/Documents/$DATABASE_FILE_NAME)
//START:code.DatabaseShoppingList.findDocumentsDirectory
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = ;
//查看文件目录
NSLog(@"%@",documentFolderPath);
NSString* dbFilePath = ;
std::string *dbPathStr = new std::string();
Layout::sharedLayout()->setDBPath(*dbPathStr);
//END:code.DatabaseShoppingList.findDocumentsDirectory
;
//START:code.DatabaseShoppingList.copyDatabaseFileToDocuments
if (! fileExistsAtPath: dbFilePath])
{
// didn't find db, need to copy
NSString *backupDbPath = pathForResource:@"News" ofType:@"db3"];
if (backupDbPath == nil)
{
// couldn't find backup db to copy, bail
return NO;
}
else
{
BOOL copiedBackupDb = copyItemAtPath:backupDbPath toPath:dbFilePath error:nil];
if (! copiedBackupDb)
{
// copying backup db failed, bail
return NO;
}
}
}
return YES;
//END:code.DatabaseShoppingList.copyDatabaseFileToDocuments
NSLog (@"bottom of initializeDb");
}
上面这段代码是app初始化的时候执行的。用一个全局的对象把得到的document的路径保存起来了,方便后面读写使用。
const char* path = Layout::sharedLayout()->getDBPath().c_str();
int iRet = 0;
sqlite3 *db;
iRet = sqlite3_open(path, &db);
if(iRet != SQLITE_OK)
{
sqlite3_close(db);
return;
}
sqlite3_stmt * stmt;
CCString *stmtString = CCString::createWithFormat("UPDATE News SET Collect = %d WHERE ID = %d", m_isCollected,m_newsID);
sqlite3_prepare_v2(db,stmtString->getCString(),-1,&stmt,NULL);
iRet = sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(db);
这段代码是在后面update数据的。
在模拟器上和真机上都试过了,都不能写入数据库,得到的结果都是SQLITE_BUSY.
请问这是什么原因呢?