Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从sqlite数据库获取数据-ios_Ios_Sqlite - Fatal编程技术网

从sqlite数据库获取数据-ios

从sqlite数据库获取数据-ios,ios,sqlite,Ios,Sqlite,我的数据库表中有5条记录,我需要在NSLog中显示第四条记录,但它不会进入代码的语句中。有人能指出我错在哪里吗 这是我的密码 -(void) readFromDatabase { [self checkAndCreateDatabase]; // Setup the database object sqlite3 *database; // Open the database from the users filessytem if(sqlite3

我的数据库表中有5条记录,我需要在
NSLog
中显示第四条记录,但它不会进入代码的
语句中。有人能指出我错在哪里吗

这是我的密码

-(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);

}

提前感谢。

您是从
文档
文件夹获取数据库的吗?如果是,则在执行代码之前检查
数据库
是否正确复制到文档文件夹中

- (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"];

哪个if语句?您确定1)数据库在databasePath中存在吗?2) 数据库路径不是零?3) 数据库包含所需数据?检查nslog获取数据或不记录数据。检查数据库路径。如果数据库路径正确,请检查表名和query@MidhunMP在我的第二个if语句中,即这里的if(sqlite3\u prepare(database,sqlStatement,-1,&compiledStatement,NULL)=SQLITE\u OK)@Akshea:检查数据库中是否存在
QuestionAnswers
表。是的,数据库在databasePath中存在。我已经检查了它…在IF prepared语句中,它不在下面的行中,如果(sqlite3_prepare(database,sqlStatement,-1,&compiledStatement,NULL)=SQLITE_OK)当我在查询中给出where条件时,这个问题发生了。如果我没有提到任何where条件,它就会进入If语句。那么,如何查询它,然后手动打开数据库并检查它是否被刷新。