Hey Folks,
I'm developing for iphone-sdk 2.2.1 (so no CoreData cry).
So I'm using the FMDatabase project, which is just a SQLite Wrapper in Obj C. I have my DB working well when interacting with it from my AppDelegate. I've tested connecting, INSERTING, etc from the AppDelegate.
Now, I have a data object that I want to store into the Database. I would like this event to occur from a ViewController class. Lets see some code:
App Delegate Creates the DB in the applicationDidFinishLaunching method. The DB is declared as a property of this class so I can easily access it.
db = [FMDatabase databaseWithPath:[self getDBPath]];
This works fine, I have tested it. The only thing I'm a little concerned about is 'db' being a property of the class. That shouldn't cause problems, should it?
From the same applicationDidFinishLaunching method, I've tested that a simple insert works using. This Works:
[db beginTransaction];
[db executeUpdate:@"INSERT INTO tblDataSamples (...) VALUES (...);"];
[db commit];
Now, if I simply take this exact code and move it to an instance method of the AppDelegate, the code no longer works. I get a "EXC_BAD_ACCESS"
error when we hit the [db beginTransaction] line.
The flow of this code to this call: - AltViewController Receives a button tap Event - AltViewController tells ApplicationDelegate to execute the "addSample" method. - ApplicationDelegate's addSample method fails on [db beginTransaction].
Now that I've written this out, I am thinking the problem is that my DB gets lost after the ApplicationDelegate loads the RootView. Maybe I'm wrong though. Anyone have any ideas?
!!!!!!!!!
UPDATE! I just altered my addSample method to include
db = [FMDatabase databaseWithPath:[self getDBPath]];
if (![db open]) {
NSLog(@"Could not open db.");
}
and
[db close];
The entire transaction works now. So, the new question is: how do I make the database open ONCE and stay open across different views and viewControllers?