Reputation: 547
I have an iPad app that is using a sqlite database using FMDB as a wrapper. I am able to successfully query data from the database; however when I attempt to insert data using executeUpdate, the method fires correctly (i.e. I get no errors and it returns YES) but nothing is actually inserted into the database. I try to immediately retrieve the object I just inserted and it is null. I also try to look at the number of rows in the table I inserted to and the table is empty. Any idea what I'm doing wrong? Here's some of the code:
--init method of the class that handles all my db work
-(NSString *)getDbPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"iRecipeBox.db"];
}
-(id)init {
self = [super init];
if(self) {
database = [FMDatabase databaseWithPath:[self getDbPath]];
[database open];
}
return self;
}
--App delegate code that moves my db into the docs directory
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docsPath = [documentsDirectory stringByAppendingPathComponent:@"iRecipeBox.db"];
if ([fileManager fileExistsAtPath:docsPath] == NO) {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"iRecipeBox" ofType:@"db"];
[fileManager copyItemAtPath:resourcePath toPath:docsPath error:&error];
}
return YES;
}
--Code to insert data into one of the tables
recipeID = [NSNumber numberWithInt:newRecipeID];
[database beginTransaction];
NSArray *newRow = [[NSArray alloc] initWithObjects:recipeID,title, description, picFile, prepTime, cookTime, ovenTemp, nil];
NSString *sql = @"insert into recipes (id, name, descr, picFile, prepTime, cookTime, ovenTemp) values (?, ?, ?, ?, ?, ?, ?)";
NSLog(@"Before update, the number of args is %i", [newRow count]);
if([database executeUpdate:sql withArgumentsInArray:newRow]) {
[database commit];
NSLog(@"the insert worked");
} else {
[database rollback];
NSLog(@"the insert failed");
}
Upvotes: 1
Views: 1465
Reputation: 2916
[FMDatabase databaseWithPath:] returns an autoreleased object, so you're going to want to retain that if you're not using ARC.
Check [database lastError] right after the executeUpdate:, and see if it says anything.
And finally- there's no point in using a transaction if it's just a single insert.
Upvotes: 1