【问题标题】:Cannot achieve a successful sqlite query to display in my text view无法在我的文本视图中显示成功的 sqlite 查询
【发布时间】:2012-05-29 02:29:33
【问题描述】:

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

 - (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 question, answer0, answer1, answer2, answer3 FROM questions

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

【问题讨论】:

    标签: objective-c ios xcode


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 2012-09-19
      • 2014-11-28
      相关资源
      最近更新 更多