FMDB Resultset返回nil

时间:2011-06-22 19:36:11

标签: objective-c uitableview sqlite where-clause fmdb

所以这是交易,我正在为我的数据库应用程序使用FMDB SQLite Wrapper。我知道Cord Data就在那里,但我的数据库真的非常庞大,而我想做的就是阅读它。没有新的记录将通过iOS应用程序插入/更新。在这个应用程序的某一点上,我试图从DB中提取记录,这些记录是基于searchTerm和searchPosition的玩家名称和他们的Id。 searchTerm工作得非常好。 searchPosition查询适用于sqlite3但不适用于iOS应用。我不知道我哪里错了。帮帮我。两种方法的代码如下:

-(IBAction)searchTerm
{
searchTermValue=self.term.text;
self.fmdbUtils = [[[FMDBUtils alloc] initWithDatabase:@"Colts.sql"] autorelease];
FMDatabase *db = [self.fmdbUtils sharedDB];
FMResultSet *rs = nil;
NSString *AgentId = nil;
NSString *PlayerName = nil;
NSString *PlayerId = nil;
ScoutAppDelegate *appDelegate = (ScoutAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.playerList = [[NSMutableArray alloc] init];

NSString * query = @"SELECT * FROM Player WHERE LENGTH (lastname)>0 AND LENGTH (firstname)>0 AND ( lastname LIKE '%' || ? || '%' OR firstname LIKE'%' || ? || '%' OR Height LIKE '%' || ? || '%' OR Weight LIKE '%' || ? || '%') ORDER BY lastname,firstname";
rs = [db executeQuery:query,searchTermValue,searchTermValue,searchTermValue,searchTermValue];
[query release];

while([rs next]) 
{
    PlayerId = [rs stringForColumn:@"player_id"];
    //NSString *PlayerName = [rs stringForColumn:@"Player Name"];
    if ([rs stringForColumn:@"Player Name"]!= NULL) 
    {
        PlayerName = [rs stringForColumn:@"Player Name"];
    }
    else
        PlayerName=@"";
    if ([rs stringForColumn:@"agent_id"]!= NULL) 
    {
        AgentId = [rs stringForColumn:@"agent_id"];
    }
    else
        AgentId=@"0";

    Player *temp =[[Player alloc]initPlayerID:PlayerId Name:PlayerName Agent:AgentId];
    [appDelegate.playerList addObject:temp];
    [temp release];
}
[rs close];
[db close];


PlayersListTableViewController *temp=[[PlayersListTableViewController alloc]initWithNibName:@"PlayersListTableViewController" bundle:nil];
temp.title=@"Players";
self.playersListTableViewCtrl=temp;
[temp release];

[self.navigationController pushViewController:playersListTableViewCtrl animated:YES];
}

以下是用于根据玩家的位置搜索玩家的信息。

-(IBAction)SearchPosition
{
searchTermValue= searchPositionValue;
self.fmdbUtils = [[[FMDBUtils alloc] initWithDatabase:@"Colts.sql"] autorelease];
FMDatabase *db = [self.fmdbUtils sharedDB];
FMResultSet *rs =  nil;
NSString *AgentId = nil;
NSString *PlayerName = nil;
NSString *PlayerId = nil;
ScoutAppDelegate *appDelegate = (ScoutAppDelegate *)[[UIApplication sharedApplication] delegate];
//appDelegate.playerList = [[NSMutableArray alloc] init];

NSString * query = @"SELECT * FROM Player WHERE LENGTH (lastname)>0 AND LENGTH (firstname)>0 AND ( College_Position LIKE '%' || ? || '%' OR Pro_Position LIKE'%' || ? || '%') ORDER BY lastname,firstname";
rs = [db executeQuery:query,searchPositionValue,searchPositionValue];
[query release];

while([rs next]) 
{
    PlayerId = [rs stringForColumn:@"player_id"];
    //NSString *PlayerName = [rs stringForColumn:@"Player Name"];
    if ([rs stringForColumn:@"Player Name"]!= NULL) 
    {
        PlayerName = [rs stringForColumn:@"Player Name"];
    }
    else
        PlayerName=@"";
    if ([rs stringForColumn:@"agent_id"]!= NULL) 
    {
        AgentId = [rs stringForColumn:@"agent_id"];
    }
    else
        AgentId=@"0";

    Player *temp =[[Player alloc]initPlayerID:PlayerId Name:PlayerName Agent:AgentId];
    [appDelegate.playerList addObject:temp];
    [temp release];
}
[rs close];
[db close];

PlayersListTableViewController *temp=[[PlayersListTableViewController alloc]initWithNibName:@"PlayersListTableViewController" bundle:nil];
temp.title=@"Players";
self.playersListTableViewCtrl=temp;
[temp release];

[self.navigationController pushViewController:playersListTableViewCtrl animated:YES];
}

此方法仅为pickerview中的搜索提供选定的游戏位置。

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
self.searchPositionValue = [playingPositions objectAtIndex: row];
}

1 个答案:

答案 0 :(得分:8)

尝试通过打印错误来调试SQL-Statement(在executeQuery:call之后):

if ([db hadError]) {
  NSLog(@"DB Error %d: %@", [db lastErrorCode], [db lastErrorMessage]); 
}

对于开发,我建议启用错误的一般记录:

db.logsErrors = YES;