Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-25-2012, 07:29 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 2
Stahilov_v is on a distinguished road
Default FMDatabase INSERT issue

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)addProjectProject*)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];


}
Stahilov_v is offline   Reply With Quote
Reply

Bookmarks

Tags
fmdatabase, insert, ios, sqlite

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 396
11 members and 385 guests
Atatator, condor304, FrankWeller, glenn_sayers, iphonedevshani, MAMN84, mraalex, PowerGoofy, QuantumDoja, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:52 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0