ios - 从sqlite数据库获取数据-iOS

标签 ios sqlite

我的数据库表中有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/

相关文章:

android - 如何在 Android 中使用 ContentResolver 进行不区分大小写的查询?

ios - 如何解决 "code couldn' t 找到匹配 'com.example.hello' 的配置文件。”?

ios - 如何从我的用户变量控制 UI 选项卡栏外观

android - 如何在基于Xamarin.forms的webview中使用html5视频标签?

iOS 8 无法连接到 BLE。可以连接到 iOS 9

ios - 在 Xcode iOS SDK 中读取 SQLite

iphone - CGContextDrawPDFPage锁定并且不返回(无限循环)

javascript - 在node js中的sqlite数据库中批量插入类对象

python - sqlite3 命令行工具在 Ubuntu 中不起作用

android - 如何计算 SQLITE 表中的列数?