Ketan Shinde
Ketan Shinde

Reputation: 1847

problem in inserting the vlaues into database

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.

Upvotes: 1

Views: 231

Answers (2)

Chetan Bhalara
Chetan Bhalara

Reputation: 10344

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

Upvotes: 0

user207616
user207616

Reputation:

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.

Upvotes: 0

Related Questions