2011-03-24 41 views
0

在我的应用程序在一个循环中打开数据库大量的时间,然后再次打开我的数据库,它不会执行if (sqlite3_open([[self getDBPath] UTF8String], &database) == SQLITE_OK)这种说法使我的应用程序不工作....数据库应用程序无法正常工作

-(NSString *)getDBPath 
{ 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 

    return [documentsDir stringByAppendingPathComponent:@"BasicGreetings.sqlite"]; 

} 



-(void) fillimage:(NSInteger)imgno 
{ 
    testAppDelegate *appDelg = (testAppDelegate *)[[UIApplication sharedApplication]delegate]; 

    sqlite3 *database= nil; 

    if (sqlite3_open([[self getDBPath] UTF8String], &database) == SQLITE_OK) 
    { 

     const char *sql = "select setflag from Tblsetflag where imgNo = ?"; 
     sqlite3_stmt *selectStmt = nil; 

     if (sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK) 
     { 
      sqlite3_bind_int(selectStmt, 1,imgno); 

      while (sqlite3_step(selectStmt) == SQLITE_ROW) 
      { 
       appDelg.a = sqlite3_column_int(selectStmt, 0); 
       NSLog(@"%d",appDelg.a); 
      } 
     } 


    } 
    sqlite3_close(database); 



} 

回答

3

你可以将代码发布到getDBPath,我怀疑它找不到该文件...检查你从该方法返回的值(在模拟器中运行)并浏览Finder中的模拟器文档目录并验证文件是否存在。

+0

我把上面的getDBpath方法....和.sqlite文件在应用程序文件夹中可用... – 2011-03-24 12:35:47

+0

Thanx给我一个建议.... – 2011-03-25 07:17:47

1

不要在每次通过-fillimage:方法时打开和关闭数据库。您应该在应用程序开始时打开它(或者当您的应用程序从后台变为活动状态时),并在应用程序退出或转到后台时关闭它。

你也永远不能最终确定你准备好的陈述,这可能导致sqlite3_close()失败。再次,您可能需要创建一次预准备语句,然后在每次运行该方法时重置该语句,并在SQLite数据库关闭之前完成它。

+0

Thanx给我一个建议.... – 2011-03-28 04:26:42

相关问题