Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving and retrieving images to and from SQLite in iOS

I am trying to save images in to Sqlite and then load image in to an UIImageView. But it's not getting work. I don't what's getting wrong. Here is the code I am using. Can anybody help me to solve this issue.

- (void)saveImage {
    sqlite3_stmt *compiledStmt;
    if(sqlite3_open([dbPath UTF8String], &db)==SQLITE_OK){
        const char *insertSQL="insert into Image(Image)values(?)";
        if(sqlite3_prepare_v2(db,insertSQL, -1, &compiledStmt, NULL) == SQLITE_OK){
            UIImage *image = [UIImage imageNamed:@"farmhouse.png"];
            NSData *imageData=UIImagePNGRepresentation(image);
            sqlite3_bind_blob(compiledStmt, 1, [imageData bytes], [imageData length], NULL);

            sqlite3_step(compiledStmt);

            char *errMsg;
            sqlite3_exec(db, insertSQL, NULL,compiledStmt,&errMsg);

        }
    }
}

- (void)showImage {
    sqlite3_stmt *compiledStmt;
    if(sqlite3_open([dbPath UTF8String], &db)==SQLITE_OK){
        const char *insertSQL = "Select Image from Image Where Sl.No = ?";
        sqlite3_prepare_v2(db,insertSQL, -1, &compiledStmt, NULL);
        if(sqlite3_prepare_v2(db,insertSQL, -1, &compiledStmt, NULL) == SQLITE_OK){
            sqlite3_bind_int(compiledStmt, 1, 1);
            if(SQLITE_DONE != sqlite3_step(compiledStmt)) {                
                NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStmt, 1) length:sqlite3_column_bytes(compiledStmt, 1)];

                if(data == nil)
                    NSLog(@"No image found.");
                else
                    imgView.image = [UIImage imageWithData:data];


            }
        }
    }
}

Can anyone point out the issue with above code.

Thanks in advance

like image 790
sree_iphonedev Avatar asked Jun 05 '12 08:06

sree_iphonedev


People also ask

Does SQLite work on iOS?

SQLite is available by default on iOS. In fact, if you've used Core Data before, you've already used SQLite.

Where is SQLite database stored iOS?

The database that can be used by apps in iOS (and also used by iOS) is called SQLite, and it's a relational database. It is contained in a C-library that is embedded to the app that is about to use it. Note that it does not consist of a separate service or daemon running on the background and attached to the app.


1 Answers

- (void)saveImage 
{
  sqlite3_stmt *compiledStmt;
  sqlite3 *db;
  if(sqlite3_open([dbPath UTF8String], &db)==SQLITE_OK){
  NSString *insertSQL=@"insert into Image(image) VALUES(?)";
  if(sqlite3_prepare_v2(db,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStmt, NULL) == SQLITE_OK){
  UIImage *image = [UIImage imageNamed:@"vegextra.png"];
  NSData *imageData=UIImagePNGRepresentation(image);

  sqlite3_bind_blob(compiledStmt, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);

  if(sqlite3_step(compiledStmt) != SQLITE_DONE ) {
      NSLog( @"Error: %s", sqlite3_errmsg(db) );
  } else {
      NSLog( @"Insert into row id = %lld", (sqlite3_last_insert_rowid(db)));
  }

sqlite3_finalize(compiledStmt);
  }
 }
sqlite3_close(db);
}

- (void)showImage
 {
   sqlite3_stmt *compiledStmt;
   sqlite3 *db;
   int i = 1;
   if(sqlite3_open([dbPath UTF8String], &db)==SQLITE_OK){
   NSString *insertSQL = [NSString stringWithFormat:@"Select image from Image Where Id = %d",i];
   if(sqlite3_prepare_v2(db,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStmt, NULL) == SQLITE_OK) {
   while(sqlite3_step(compiledStmt) == SQLITE_ROW) {

   int length = sqlite3_column_bytes(compiledStmt, 0);
   NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(compiledStmt, 0) length:length];

   NSLog(@"Length : %d", [imageData length]);

  if(imageData == nil)
     NSLog(@"No image found.");
  else
     imgView.image = [UIImage imageWithData:imageData];
   }
 }
 sqlite3_finalize(compiledStmt);
}
sqlite3_close(db);
}
like image 145
Hector Avatar answered Nov 15 '22 11:11

Hector