开发者

FMDB Result set ruturning nil

开发者 https://www.devze.com 2023-03-14 22:36 出处:网络
So here is the deal, I am using FMDB SQLite Wrapper for my DB app. I know Cord Data is there but My database is really really huge and all I want to do is read it. No new records are going to be inser

So here is the deal, I am using FMDB SQLite Wrapper for my DB app. I know Cord Data is there but My database is really really huge and all I want to do is read it. No new records are going to be inserted / updated via iOS app. At one point in this a开发者_开发技巧pp I am trying to pull records from DB which are name of player and thier Id based on searchTerm and searchPosition. searchTerm works perfectly fine. searchPosition query works in sqlite3 but not in iOS app. I don know where I am going wrong. Help me out. Code for both methods are as follows:

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

Here is theIBAction used to search players based on their playing position.

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

This method just gives selected playing position for search in pickerview.

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


Try to debug the SQL-Statement (after the executeQuery: call) by printing the error:

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

For developing I would recommend to enable general logging of errors:

db.logsErrors = YES;
0

精彩评论

暂无评论...
验证码 换一张
取 消