2013-03-29 51 views
0

我总是使用这段代码在我的iPhone应用程序中打开一个sqlite数据库,但现在使用iOS6,此代码不起作用。函数:sqlite3_prepare_v2失败,但我不明白为什么!iOS 6:SQLite数据库不起作用

我也不明白为什么如果我尝试更改名称:“gamer.sqlite”的sqlite数据库与另一个不存在的文件名称,函数:'sqlite3_open'再次返回SQLITE_OK值。

这是我的代码:

-(NSMutableArray*)view_table 
{ 
    const char *sql; 

    NSMutableArray *content=[[NSMutableArray alloc] initWithObjects:nil]; 
    [self msgbox:@"eseguo"]; 
    /*elementi da visualizzare nella tabella*/ 
    if(sqlite3_open([databasePath UTF8String],&database)==SQLITE_OK) 
    { 
      [self msgbox:@"opened"]; 
     sql = "SELECT * FROM list_gamer"; 
     if (sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL) == SQLITE_OK) 
     { 
       [self msgbox:@"query eseguita"]; 
      while(sqlite3_step(selectstmt) == SQLITE_ROW) 
      { 
       [content addObject:[NSString stringWithFormat:@"%s",sqlite3_column_text(selectstmt,0)]]; 
      } 
     } 
    } 

    return content; 

} 


- (void)viewDidLoad{ 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [[documentsDir stringByAppendingPathComponent:@"gamer.sqlite"] copy]; 

    if(![[NSFileManager defaultManager] fileExistsAtPath:databasePath]) 
    { 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     int success = [fileManager fileExistsAtPath:databasePath]; 
     if(success) 
     { 
      [fileManager removeItemAtPath:databasePath error:nil]; 
      return; 
     } 

     NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"gamer.sqlite"]; 

     // Copy the database from the package to the users filesystem 
     [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 


     documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     documentsDir = [documentPaths objectAtIndex:0]; 
    } 

[super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning{ 
    [super didReceiveMemoryWarning]; 

} 

-(IBAction)start_game:(id)sender{ 
    self.view=frmgame; 
    [self view_table]; 
} 
+0

你需要从数据库中获取的错误。添加一个处理prepare语句失败的'else'。使用'sqlite3_errmsg'获取错误,然后记录错误消息。 – rmaddy

回答

0

不完全是一个直接的答案,但我个人很喜欢使用FMBD来帮助管理SQLite &错误。

FMDB是围绕SQLite的一个Objective-C包装:http://sqlite.org/

https://github.com/ccgus/fmdb

+0

我读了这个sqlite包装器的参考,它非常简单。我想用它。 –

+0

感谢upvote我的答案。 – Franck