user564963
user564963

Reputation: 2294

problem in reading image from data base in iphone

hi i am able to store the image into database by using the following code.......

-(void)insertimages: (NSData *)image
{
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        NSUInteger len = [image length];
        NSLog(@"data size is %d", len);

        sqlStatement="insert into messages values(10,?)";
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
        {
            //sqlite3_bind_int(compiledStatement, 1, -1);
            //sqlite3_bind_blob(updateStmt, 3, [imgData bytes], [imgData length], NULL);
            sqlite3_bind_blob(compiledStatement, 1, [image bytes], [image length], SQLITE_TRANSIENT);
            //sqlite3_bind_text(compiledStatement, 1, [SMSBody UTF8String],-1,SQLITE_STATIC);
            if(sqlite3_step(compiledStatement) == SQLITE_DONE) 
            {
                sqlite3_finalize(compiledStatement);                
            }

        }       
        sqlite3_close(database);    
    }   
}

but now my problem is i am not able to retrieve the image from the database and to display it in uiimageview...

con any one please help me how to do that.

Upvotes: 1

Views: 397

Answers (2)

bigkm
bigkm

Reputation: 2277

I was trying to do this a year ago, I wrote code to save the images into a sqlite3 db using osx sdk (of which i was able to write and read, i could even store the images in the db with ruby and read them on with macosx sdk). But when i moved this exact code over to an iOS app it would pull out bad data. If you do work this out i'd like to know the answer.

Upvotes: 0

Mayur Birari
Mayur Birari

Reputation: 5835

Use following method to read the image data from database and stored the images in array or in dictionary


-(void) readImageDataFromDatabase {
    // Setup the database object
    sqlite3 *database;

    // Init the img Array
    imageArray = [[NSMutableArray alloc] init];

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement = "select * from messages";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // Read the data from the result row
                NSURL *url = [NSURL URLWithString:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]];
];
                NSData *imgData = [NSData dataWithContentsOfURL: url];

                // Create a new image object with the data from the database
                iconImage.image=[UIImage imageWithData:imgData];

                // Add the img object to the img Array
                [imageArray addObject:iconImage];
            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);

}

Upvotes: 1

Related Questions