2011-07-18 8 views
0

SQLiteファイルからデータを取得しようとしていますが、このコードをテストするビューが1つしかない多くのビューベースのアプリケーションを作成しており、毎回完璧に動作します。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と複数のビューがありますが、これは私が考えることができる唯一のものです。これは時間の経過とともにこれとは異なります。私は1つのビューでテストしました。

ありがとうございます。 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データベースの名前を変更して修正しました。

関連する問題