0

我的 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 语句。任何帮助,将不胜感激。

4

3 回答 3

1

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

于 2012-06-25T17:55:28.233 回答
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);
}
于 2012-06-21T05:30:32.200 回答
-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);
}

通过它它会工作

于 2012-06-21T05:59:15.900 回答