我正在尝试在文本视图中显示一串文本。根据 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] 搞砸了查询!