sqlite3_prepare_v2语句不是SQLITE_OK,我不知道为什么

时间:2012-06-20 23:43:59

标签: iphone objective-c if-statement sqlite

我在viewWillAppear方法中有这个代码:

 sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String], &database)
    != SQLITE_OK) { 

    sqlite3_close(database);
    NSAssert(0, @"Failed to open database");
}
sqlite3_stmt *statement;

//why is this if statement failing?
if (sqlite3_prepare_v2(database, [sqlStatement UTF8String],
                       -1, &statement, nil) == SQLITE_OK) {

它传递第一个if语句而没有输入(这是好的)。第二个if语句是问题所在。

sqlStatement的形式为SELECT * FROM food WHERE foodType ='fruit' 我不明白为什么它没有进入if语句。任何帮助,将不胜感激。

3 个答案:

答案 0 :(得分:1)

问题最终没有出现在代码中,而是出现了我导出sqlite文件的方式。这是关于INSERT语句的系列文章,而不是实际的表格。

答案 1 :(得分:0)

并根据您的要求进行更改......

-(void) readDataFromDatabase
{
    chapterAry = [[NSMutableArray alloc] init];

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDir = [documentPaths objectAtIndex:0];

    databasePath = [documentsDir stringByAppendingPathComponent:@"QuotesApp.sqlite"];

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        const char *sqlStatement ="select * from MsExcelTutorial";

        sqlite3_stmt *compiledStatement;

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
        {
            while(sqlite3_step(compiledStatement)==SQLITE_ROW)
            {
                NSNumber *chapterId = [NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 0)];

                NSString *chapter = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

                NSString *link = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

                NSString *bookmark = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

                NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:chapterId,@"chapterId",chapter,@"chapter",link,@"link",bookmark,@"bookmark", nil];

                [chapterAry addObject:dic];
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}

答案 2 :(得分:-1)

NSString *DBPath = [ClsCommonFunctions GetDatabasePath];
if ([self OpenDBWithPath:DBPath])//Method to open database connection
{
//filesDB is database object 
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "select fileID,filePath from tblFiles where  isUploadPending =1";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(filesDB, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

        while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
        {
            // u Need to get data from database...                


        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);
    sqlite3_close(filesDB);
}

通过它它会工作