0

我正在开发一个 iPhone 应用程序。我创建了一个可重用的类,其中编写了一个 sqlite getData 方法。我想从我的控制器传递一个 sql 语句,并希望得到一个包含所有行的数组。

我可以将 sqlite3_stmt 对象存储到数组中并返回该数组,所以在调用点我可以转换它并找出每个列的值吗?

我当前的代码是这样的:

-(NSMutableArray*)getData:(NSString*) SqlQuery
{    
// The Database is stoed in the application bundle
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"sqliteClasses.sqlite"];

if(sqlite3_open([path UTF8String], &contactDB) == SQLITE_OK)
{
    const char *sql = (const char*)[SqlQuery UTF8String];
    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(contactDB, sql, -1, &compiledStatement, NULL) == SQLITE_OK)
    {
        // Loop through the results and add them to the feeds array
        while (sqlite3_step(compiledStatement) == SQLITE_ROW)//(stepResult == SQLITE_ROW) 
        {
          //  [arrayRecords addObject:compiledStatement];                
        }
    }    
    sqlite3_finalize(compiledStatement);        
}
sqlite3_close(contactDB);
return arrayRecords;

}

错误行是:[arrayRecords addObject:compiledStatement];

我怎样才能做到这一点?任何替代实施这个?

谢谢。

4

1 回答 1

0
- (NSArray *) getActionWithFilters:(NSDictionary *)dictionary ClassName:(NSString *)className Error:(NSError **)error{
    NSString *query = [NSString stringWithFormat:@"SELECT * FROM %@ %@",className,[self getFitlerArrayByDictionary:dictionary]];
    sqlite3_stmt *statement = [self getItemsWithQuery:query];

    NSMutableArray *array = [[NSMutableArray alloc] init];
    while (sqlite3_step(statement) == SQLITE_ROW) {
        NSMutableDictionary *itemDic = [[NSMutableDictionary alloc] init];
        int columns = sqlite3_column_count(statement);
        for (int i=0; i<columns; i++) {
            char *name = (char *)sqlite3_column_name(statement, i);
            NSString *key = [NSString stringWithUTF8String:name];
            switch (sqlite3_column_type(statement, i)) {
                case SQLITE_INTEGER:{
                    int num = sqlite3_column_int(statement, i);
                    [itemDic setValue:[NSNumber numberWithInt:num] forKey:key];
                }
                    break;
                case SQLITE_FLOAT:{
                    float num = sqlite3_column_double(statement, i);
                    [itemDic setValue:[NSNumber numberWithFloat:num] forKey:key];
                }
                    break;
                case SQLITE3_TEXT:{
                    char *text = (char *)sqlite3_column_text(statement, i);
                    [itemDic setValue:[NSString stringWithUTF8String:text] forKey:key];
                }
                    break;
                case SQLITE_BLOB:{
                    //Need to implement
                    [itemDic setValue:@"binary" forKey:key];
                }
                    break;
                case SQLITE_NULL:{
                    [itemDic setValue:[NSNull null] forKey:key];
                }
                default:
                    break;
            }
        }
        [array addObject:itemDic];
        [itemDic release];
    }

    return [array autorelease];
}
于 2012-07-18T13:49:55.723 回答