【问题标题】:Select query not working in SQLite选择查询在 SQLite 中不起作用
【发布时间】:2012-03-18 18:23:56
【问题描述】:

我正在运行一个包含选择查询的方法,但是编译的语句不起作用,断点跳过它声明它的行,当我将光标放在它上面时它没有显示任何值,这是我正在使用的代码:

-(NSMutableArray *)GetAllPartsName
{
    NSString *path = [self getDBPath];
    // Open the database from the users filessytem

    NSMutableArray *Parts=[[[NSMutableArray alloc]init]autorelease ];
    if(sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
        // Setup the SQL Statement and compile it for faster access

        NSString *sqlQuery = [NSString stringWithFormat:@"SELECT Parts_Name FROM Parts"];
        NSLog(@"%@",sqlQuery);
        const char *sqlStatement = [sqlQuery UTF8String]; 
        //The break point skips the sqlite3_stmt.
        sqlite3_stmt *compiledStatement;
        //when i put the cursor over compiledStatement it shows no value
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // Read the data from the result row
                NSMutableDictionary *PositionDict=[[NSMutableDictionary alloc]init];
                //setting the parts into dictionary
                [PositionDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0)] forKey:@"Parts_Name"];
                [Parts addObject:PositionDict];
                NSLog(@"%@",PositionDict);
                [PositionDict release];
            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
    return Parts;
}

【问题讨论】:

    标签: iphone ios xcode sqlite


    【解决方案1】:

    试试 FMDattabase (https://github.com/ccgus/fmdb)

    基本:

        FMDatabase *_db = [[FMDatabase databaseWithPath:[[self class] databaseFilePath]] retain];
        [_db open];
    
    NSMutableArray *emails = [[NSMutableArray alloc] init];
    NSString * query = [NSString stringWithFormat:@"SELECT `email` FROM `email`"];
    FMResultSet * result = [_db executeQuery:query];
    while ([result next]) 
    {
        [emails addObject:[result stringForColumn:@"email"]];
    }
    return [emails autorelease];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-09
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      相关资源
      最近更新 更多