我正在开发一款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];
我怎样才能做到这一点?实现这个的任何替代方案?
感谢。
答案 0 :(得分: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];
}