我的数据库表中有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);
}
提前致谢。
最佳答案
您是从document
文件夹中获取数据库吗?如果是,则在执行代码之前检查database
是否正确复制到Document文件夹中。
- (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"];
关于ios - 从sqlite数据库获取数据-iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18628661/