Hi guys,
I've run into a simple complication, I dont think i got how FMDatabase can be initialised and used throughout the application in similar ways of Sqlite.
For example,
The above code works perfectly fine, when called from anywhere..
However, If I actually Synthesize the FMDatabase in appDidFinishLaunching, and utilise that database for various other uses, it fails.
in AppDelegate.h
in AppDelegate.m
I thought atleast, theoretically this would work, but it didnt.
When i called -doSomethingWithDb at runtime, i got the error:
objc_msgSend
and this is the line it highlights,
theres no mention of what else is wrong, in console.
Whats going wrong here? There arent too many examples of how FMDb is to be created initialised. As I plan to call it from many other viewControllers.
Any suggestion is appreciated.
But the code at the top, where a whole new FMDatabase is initialised everytime during the app works perfectly, but theres a certain lag which is either cuz of the FM wrapper itself, i read somewhere about that, or its because of the database being initialised and opened at every call.
I've run into a simple complication, I dont think i got how FMDatabase can be initialised and used throughout the application in similar ways of Sqlite.
For example,
Code:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
FMDatabase *newDB = [FMDatabase databaseWithPath:path];
if (![newDB open]) {
}
FMResultSet *rs = [newDB executeQuery:@"select * from tap where id=?", @"2"];
if ([newDB hadError]) {
NSLog(@"Err %d: %@", [newDB lastErrorCode], [newDB lastErrorMessage]);
}
while ([rs next]) {
NSLog(@"%@", [rs stringForColumn:@"Name"]);
}
[rs close];
[newDB close];
The above code works perfectly fine, when called from anywhere..
However, If I actually Synthesize the FMDatabase in appDidFinishLaunching, and utilise that database for various other uses, it fails.
in AppDelegate.h
Code:
FMDatabase *DB;
@property(nonatomic, retain) FMDatabase *DB;
in AppDelegate.m
Code:
@synthesize DB;
-(void)applicationDidFinishLaunching{
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
DB = [FMDatabase databaseWithPath:path];
if (![DB open]) {
NSLog(@"cannot open db");
}
}
-(void)doSomethingWithDb{
FMResultSet *rs = [DB executeQuery:@"select * from tap where id=?", @"2"];
if ([newDB hadError]) {
NSLog(@"Err %d: %@", [DB lastErrorCode], [DB lastErrorMessage]);
}
while ([rs next]) {
NSLog(@"%@", [rs stringForColumn:@"Name"]);
}
[rs close];
}
When i called -doSomethingWithDb at runtime, i got the error:
objc_msgSend
and this is the line it highlights,
Code:
FMResultSet *rs = [theFileb executeQuery:@"select * from tap where id=?", @"2"];
Whats going wrong here? There arent too many examples of how FMDb is to be created initialised. As I plan to call it from many other viewControllers.
Any suggestion is appreciated.
But the code at the top, where a whole new FMDatabase is initialised everytime during the app works perfectly, but theres a certain lag which is either cuz of the FM wrapper itself, i read somewhere about that, or its because of the database being initialised and opened at every call.