2011-07-18 57 views
0

我想從SQLite文件中獲取數據,我創建了許多基於視圖的應用程序,只有一個視圖來測試此代碼,並且它每次都運行完美。SQLite不拉數據

但由於某些原因,當有多個視圖不通過運行所有的方式或者給在這裏的任何錯誤是代碼:

TorD_Questions.h

#import <UIKit/UIKit.h> 
    #import <sqlite3.h> 

@interface TorD_Questions : UIViewController 
{ 

    sqlite3   *database; 

} 

- (void) MakeDBCopy; 
- (void) GetQuestion; 


@end 

TorD_Questions .m

#import "I_Dare_YouViewController.h" 
#import "About_Page.h" 
#import "Settings_Page.h" 
#import "Game_Info.h" 
#import "Game_TDoR.h" 
#import "Settings_Menu.h" 
#import "TorD_Questions.h" 


@implementation TorD_Questions 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self MakeDBCopy]; 
    [self GetQuestion]; 
} 

NSString *fileName = @"Questions.sqlite"; 


- (void) MakeDBCopy 
{ 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:fileName]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) 
    { 
     return; 
    } 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) 
    { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 

- (void) GetQuestion 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName]; 
    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) 
    { 

     const char *sql = "select * from Truth"; 

     sqlite3_stmt *searchStatement; 

     if (sqlite3_prepare_v2(database, sql, -1, &searchStatement, NULL) == SQLITE_OK) 
     { 
      while (sqlite3_step(searchStatement) == SQLITE_ROW) 
      { 
       NSString *TheID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 0)]; 
       NSString *TheQuestion = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 1)]; 
       NSLog(@"ID: %@ Question: %@", TheID, TheQuestion); 
      } 
     } 
     sqlite3_finalize(searchStatement); 
    } 
} 

@end 

如果有人知道sqlite的問題和不止一個觀點,因爲這是我能想到的唯一事情,在這個時代裏,我只用一種觀點對它進行了測試。

預先感謝您。 Eli

+2

只有受虐狂直接在Objective-C中使用SQLite C API。 [使用FMDB](http://github.com/ccgus/fmdb)(一個SQLite包裝器)或[CoreData](http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/ cdProgrammingGuide.html)(一個對象圖管理器)。 –

回答

0

問題解決了,我通過重命名SQlite數據庫來修復它。