在xcode中的SQLite中执行查询

时间:2012-08-03 21:09:53

标签: ios database xcode sqlite nsarray

我这样做是对的,因为数组返回的计数为0。

-(NSArray *)getAllFavourites{
NSMutableArray *arrFavourites = [[NSMutableArray alloc] init];
NSString *query = @"SELECT * FROM  tbl_favourites;";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK){
    while (sqlite3_step(statement) == SQLITE_ROW ){
        char *charSymbols = (char *) sqlite3_column_text(statement, 0);
        NSString *strFetchedSymbol = [[NSString alloc] initWithUTF8String:charSymbols];
        DBClassFavourites *dbClassFav = [[DBClassFavourites alloc] initWithString:strFetchedSymbol];
        [arrFavourites addObject:dbClassFav];
    }
    sqlite3_finalize(statement);
}
NSLog(@"%i",[arrFavourites count]);
return arrFavourites;
}

我也有这个类初始化所需的变量。

 @implementation DBClassFavourites
 -(id) initWithString:(NSString *)symbol
{
self = [super init];

if (self) {
    self.strSymbol = symbol;
}
return self;
}

-(void) dealloc
{
[strSymbol release];
[super dealloc];
}

这是我的firstviewcontroller初始化函数

所有调用的地方
 @implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

 arrFavourites = [[DatabaseManager database] getAllFavourites];

}

1 个答案:

答案 0 :(得分:0)

您应该尝试使用FMDB wrapper

要在代码中查找错误,您需要发布整个数据库初始化方法