所以这是交易,我正在为我的数据库应用程序使用FMDB SQLite Wrapper。我知道那里有Cord Data,但是我的数据库确实非常庞大,我要做的就是读取它。不会通过iOS应用插入/更新任何新记录。在这个应用程序的某一时刻,我试图从数据库中提取记录,这些记录是基于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。

-(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];
}

最佳答案

尝试通过打印错误来调试SQL语句(在executeQuery:调用之后):

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


对于开发人员,我建议启用常规错误记录:

db.logsErrors = YES;

关于objective-c - FMDB结果集返回nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6445486/

10-11 23:47