我有个问题..。我有一个应用程序使用sqlite保存一些data.Everything工作完美,这意味着我可以添加,删除,查看数据。当应用程序进入后台时,数据是持久的。现在,当我从内存中删除应用程序或重新启动iPhone时,数据库已经损坏,所有数据都被破坏了!
我有一个类调用dbAccess,其中定义了所有数据库操作(添加、删除、撤回行)。最后,我有一个finalize操作,它完成所有使用的语句,然后关闭数据库。
+ (void)finalizeStatements{
NSLog(@"Finalizing the Delete Statements");
if(deleteStmt) {
NSLog(@"Delete Statement exist... finalization");
sqlite3_finalize(deleteStmt);
deleteStmt = nil;
}
NSLog(@"Finalizing the Add Statements");
if(addStmt) {
NSLog(@"Add Statement exist... finalization");
sqlite3_finalize(addStmt);
addStmt = nil;
}
NSLog(@"Finalizing the Store Statements");
if(storeStmt) {
NSLog(@"Store Statement exist... finalization");
sqlite3_finalize(storeStmt);
storeStmt = nil;
}
NSLog(@"Finalizing the Agent Statements");
if(agentStmt) {
NSLog(@"Agent Statement exist... finalization");
sqlite3_finalize(agentStmt);
agentStmt = nil;
}
NSLog(@"Closing the Database");
if(database) {
NSLog(@"The database exist... closing it");
sqlite3_close(database);
}}
应用程序委托在applicationDidEnterBackground和applicationWillTerminate时调用此方法。openDatabase方法在applicationDidBecomeActive时调用。
知道数据库为什么被破坏了吗?
谢谢。
发布于 2011-08-28 01:20:32
首先,查看fmdb或其他一些经过验证的包装器。您也可以浏览代码。
不知道是否有足够的信息知道为什么腐败。但是,您应该从所有sqlite3_xxx调用中获得返回代码,并至少登录以了解发生了什么,否则您可能只是在抛出一个问题。
此外,请确保调用sqlite_errmsg,如果返回代码不成功,这将提供更多线索。
在我的包装纸里,我做的很近。如果没有处理,预计语句将被最后处理。我有一个语句缓存,在清除结束每个语句。:
- (void)close
{
if (_sqlite3)
{
NSLog(@"closing");
[self clearStatementCache];
int rc = sqlite3_close(_sqlite3);
NSLog(@"close rc=%d", rc);
if (rc == SQLITE_BUSY)
{
NSLog(@"SQLITE_BUSY: not all statements cleanly finalized");
sqlite3_stmt *stmt;
while ((stmt = sqlite3_next_stmt(_sqlite3, 0x00)) != 0)
{
NSLog(@"finalizing stmt");
sqlite3_finalize(stmt);
}
rc = sqlite3_close(_sqlite3);
}
if (rc != SQLITE_OK)
{
NSLog(@"close not OK. rc=%d", rc);
}
_sqlite3 = NULL;
}
}https://stackoverflow.com/questions/7218432
复制相似问题