1
votes

i was getting familiar with sqlite and try to insert the values into database but giving exception. tf1 and tf2 are object of textfield. here is my code:

@synthesize tf1, tf2, add, disp;
-(id)initApp

{
if(![super init])
        return nil;

    //DB stored in application bundle
    NSString *DBName = @"kbase.sqlite";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingString:DBName];

    db = [FMDatabase databaseWithPath:path];
    [db setLogsErrors:TRUE];
    [db setTraceExecution:TRUE];

    if(![db open])
    {
        NSLog(@"Could not open Database");
        return 0;
    }

    else
    {
        NSLog(@"Database opened successfully");
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initApp];
    [add addTarget:self action:@selector(insertButtonClicked:)   forControlEvents:UIControlEventTouchUpInside]; 
    [disp addTarget:self action:@selector(displaytButtonClicked:)   forControlEvents:UIControlEventTouchUpInside]; 
}


-(IBAction)insertButtonClicked :(id)sender
{ 
    NSLog(@"insert");
    NSLog(@"db.....%@",db);
    [db executeUpdate:@"insert into info values (?,?)", tf1.text, tf2.text];

    [self resignFirstResponder];

}

-(IBAction)displayButtonClicked:(id)sender

{
    NSString *str1 = tf2.text;
    NSString *returnResult = [[NSString alloc]init];
    FMResultSet *rs = [db executeQuery:@"select name from info where rollno = 1"];

    while ([rs next])
    {
        returnResult = [rs stringForColumn:@"rollno"];
        [tf1 setText:returnResult];
        [tf2 setText:returnResult];
    }       
}

databse is opening successfully and controll terminate after printing insert exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL executeQuery:]: unrecognized selector sent to instance 0x3d30ef0'

i am not able to configure the problem. plz help.

2
show the code of executeUpdateRakesh Bhatt
As you can see, an NSURL object gets the query method. Can you dump the class name of the db before you fire your statements? And please post the code how you setup the db.Nick Weaver
what are you doing in executeUpdate method? Can you show the code?Chetan Bhalara
@rakesh loves, @chetan bhalara: i have updated my code! plz chk out the codeKetan Shinde
@chetan : i used the sqlite manager to create bd and table!Ketan Shinde

2 Answers

0
votes

I think make your method

[db executeUpdate:@"insert into info values (?,?)" withValue1:tf1.text withValue2:tf2.text];

Instead of

[db executeUpdate:@"insert into info values (?,?)", tf1.text, tf2.text];

0
votes

it seems that db does not exist in this context. If db is a class variable, make sure it is not released somewhere else. Simply put an NSLog(@"%@", db); above the execution to test it.