I have a database that has a student entity:
create table student ( name varchar(20) not null,
surname varchar(20) not null,
studentId integer primary key );
In my app delegate when the application starts I load all the tuples from the database and display them to a table view.
When the app delegate receiver the applicationWillResignActive event it saves all the tuples to the database.It saves only the tuples that have been added, there isn't the possibility to edit the tuples so that's fine.
When the app delegate receives the applicationWillEnterForeground event it reads the tuples from the database.
The problem: This works only inside a single launch of the application.Let's say that I launch the application through Xcode, then I enter I add an element and enter in background.Afterwards I reactivate the application, it goes in foreground, and it loads successfully all the tuples.
But at the next launch of the application (stopping it from lldb and launching it again) the tuples are the same as before the first launch.
I created a wrapper class for the student entity, named Student, with these properties:
@property (nonatomic, copy, readwrite) NSString* name;
@property (nonatomic, copy , readwrite) NSString* surname;
@property (nonatomic, strong, readwrite) NSNumber* studentId;
@property (nonatomic, readwrite, getter= isNew) BOOL new;
I check that the fields are not nil and that the entity integrity is not violated.
The only significative method that I use is this:
- (NSString*) sqlInsertionString;
It creates the string necessary to insert the tuple.Tested and works.
So that's how I update the database:
- (void) updateStudents : (NSMutableArray*) students
{
sqlite3* database;
sqlite3_stmt* statement;
BOOL needsFinalize=NO;
if(!databasePath)
{
NSBundle* bundle=[NSBundle mainBundle];
databasePath= [bundle pathForResource: @"test" ofType: @"db"];
}
if(sqlite3_open([databasePath UTF8String], &database)!=SQLITE_OK)
{
NSString* problem= [NSString stringWithFormat: @"%s",sqlite3_errmsg(database)];
@throw [NSException exceptionWithName: @"Failed to open the database" reason: problem userInfo: nil];
}
for(Student * s in students)
{
if(s.isNew)
{
NSString* sqlString= [s sqlInsertionString];
if(sqlite3_prepare_v2(database, [sqlString UTF8String], -1, &statement, NULL)!= SQLITE_OK)
{
@throw [NSException exceptionWithName: @"Problem performing the query" reason: @"Unknown" userInfo: nil];
}
if(sqlite3_step(statement)!=SQLITE_DONE)
{
@throw [NSException exceptionWithName: @"Problem performing the query" reason: @"Unknown" userInfo:nil];
}
sqlite3_reset(statement);
needsFinalize=YES;
}
}
if(needsFinalize && sqlite3_finalize(statement)!=SQLITE_OK)
{
@throw [NSException exceptionWithName: @"Failed to close the statement resource" reason: @"Unknown" userInfo:nil];
}
if(sqlite3_close(database)!= SQLITE_OK)
{
@throw [NSException exceptionWithName: @"Failed to close the database" reason: @"Unknown" userInfo: nil];
}
}
But it doesn't get really updated, only inside the application it appears to be updated, but the file always remains the same.