I'm making an application with database. For modal I use FMDatabase classes. Now I have to insert some new data in two tables: -project(id_proj primary key autoince unique, name, baseImage) -imagesforpro(id_img primkey autoincr unique, smallimage, largeimage, id_proj) I have a method -(void)addProject

Project*)proj for which I pass a Project object. Then I have to insert projectName and baseImage into project table and then extract the id_proj of the record I've just inserted and insert small and larges images into imagesforpro table.How to remeber the id_proj(which is autoincrement) for the last inserted record? Moreover, how to use INSERT? DO I use it properly? how to put NSData into the database properly? thanks. Here is what I have so far:
Code:
-(void)addProject:(Project*)proj
{
[db open];
NSData *bmdata=UIImagePNGRepresentation(proj.baseImage);
if (proj==nil) {
NSLog(@"Dead Object");
}
NSLog(@" NEW name is %@", proj.projectName);
//DOESN'T WORK
//id_proj is autoincrement field that's why I just pass name and base image
[db executeUpdate:@"INSERT INTO project VALUES (?,?)", [proj.projectName UTF8String], bmdata];
//sqlite3_last_insert_rowid()
[db close];
sqlite3 *dataB;
int status=sqlite3_open([[self dbPath] UTF8String], &dataB);
if(status!=SQLITE_OK)
{
NSLog(@"Error");
//exit(1);
}
//RETURNS 0(
int lastindex=sqlite3_last_insert_rowid(dataB);
NSLog(@"Last inserted index %i", lastindex);
sqlite3_close(dataB);
[db open];
//Add images for the project (another table)
for(int i=0; i<[proj.largeImages count]; i++)
{
UIImage *smallImg=[proj.smallImages objectAtIndex:i];
UIImage *bigImg=[proj.largeImages objectAtIndex:i];
NSData *sidata=UIImagePNGRepresentation(smallImg);
NSData *bidata=UIImagePNGRepresentation(bigImg);
// Here I have to insert an image and id of the project it refers too
//How do I find out the last inserted id_proj?
[db executeUpdate:@"INSERT INTO imagesforpro VALUES (?,?,?)", sidata, bidata, lastindex];
}
[db close];
}