2013-03-29 97 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