我的数据库表中有5条记录,我需要在NSLog
中显示第四条记录,但它不会进入代码的IF
语句。有人可以建议我错在哪里吗?
这是我的代码。
-(void) readFromDatabase {
[self checkAndCreateDatabase];
// Setup the database object
sqlite3 *database;
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select * from QuestionAnswers where Q.No = 4";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *questions = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *answers = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSLog(@"Questions:%@",questions);
NSLog(@"Answers:%@",answers);
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
提前致谢。
答案 0 :(得分:0)
您从document
文件夹获取数据库吗?如果是,则在执行代码之前检查database
是否已很好地复制到文档文件夹中。
- (BOOL) getFileExistence: (NSString *) filename
{
BOOL IsFileExists = NO;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *favsFilePath = [documentsDir stringByAppendingPathComponent:filename];
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
if ([fileManager fileExistsAtPath:favsFilePath])
{
IsFileExists = YES;
}
return IsFileExists;
}
BOOL isDBExists = [self getFileExistence:@"dbname"];