无法实现成功的sqlite查询以在我的文本视图中显示

时间:2012-05-28 15:33:25

标签: objective-c ios xcode

我正在尝试在文本视图中显示一串文本。根据NSLog,找到了数据库但是,我无法从表中检索数据。有人可以指出我的查询有什么问题吗? (我可能需要用非常简单的术语解释这个问题。我已经唱了几天了。)

 - (void)viewDidLoad {

 NSString *docsDir;
 NSArray *dirPaths;

 // Get the documents directory
 dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

 docsDir = [dirPaths objectAtIndex:0];

 // Build the path to the database file
 databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"trivia_game.db"]];
 NSLog(@"Full DB path: %@", databasePath);

 NSFileManager *filemgr = [NSFileManager defaultManager];

 if ([filemgr fileExistsAtPath: databasePath ] == NO)
 {
     const char *dbpath = [databasePath UTF8String];
     NSLog(@"[SQLITE] DB not found");

     } else {
     NSLog(@"[SQLITE] trivia_game.db found");
         }

 const char *dbpath = [databasePath UTF8String];
 sqlite3_stmt *statement;

 if (sqlite3_open(dbpath, &questionsForGame) == SQLITE_OK)
 {
     NSLog(@"[SQLITE] db opened");

     NSString *querySQL = [NSString stringWithFormat: @"SELECT question, answer0, answer1, answer2, answer3 FROM questions"];
     NSLog(@"[SQLITE] string is: %@", querySQL);

     const char *query_stmt = [querySQL UTF8String];


     if (sqlite3_prepare_v2(questionsForGame, query_stmt, -1, &statement, NULL) == SQLITE_OK)
     {
         NSLog(@"[SQLITE] written!");

         if (sqlite3_step(statement) == SQLITE_ROW)
         {
             NSString *textField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
             questHolder.text = textField;
             NSLog(@"[SQLITE] string is: %@",textField);




             [textField release];


         } else {
             questHolder.text = @"query not found";
             NSLog(@"[SQLITE] Unable to open database!");

         }
         sqlite3_finalize(statement);
         sqlite3_close(questionsForGame);

     } else {
         NSLog(@"[SQLITE] Screwed up query!");
         questHolder.text = @"query not found";


     }


 }


 [filemgr release];
 [super viewDidLoad];

}

日志:

[会议开始于2012-05-28 12:27:19 -0300。] 2012-05-28 12:27:20.547 ans [9374:207]完整数据库路径:/ Users / admin / Library / Application Support / iPhone Simulator / 4.3 / Applications / 0B4C50FB-5A3F-4371-83D6-1A2AF95B9F66 / Documents / trivia_game .db的

2012-05-28 12:27:20.549 ans [9374:207] [SQLITE] trivia_game.db发现

2012-05-28 12:27:20.550 ans [9374:207] [SQLITE] db打开

2012-05-28 12:27:20.551 ans [9374:207] [SQLITE]字符串是:SELECT问题,answer0,answer1,answer2,answer3 FROM questions

2012-05-28 12:27:20.553 ans [9374:207] [SQLITE]搞砸了查询!

1 个答案:

答案 0 :(得分:0)

我不熟悉您正在使用的SQLite库,但我建议您切换到Core Data。比照http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/coredata/cdprogrammingguide.html