title: How to store image in sqlite database in iphone?
body: <pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *path = [documentsDirectory stringByAppendingPathComponent:@"image.sqlite"];
  
  
      if (sqlite3_open_v2([path UTF8String], &amp;database, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK)
      {
  
  
          if(selectStmt == nil)
          {
              const char *sql = "insert into imagetb(imagename,image) Values(?,?)";
              if(sqlite3_prepare_v2(database, sql, -1, &amp;selectStmt, NULL) != SQLITE_OK)
                  //              NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
                  NSLog(@"%s",sqlite3_errmsg(database));
          }
  
  
  
          sqlite3_bind_text(selectStmt, 1, [@"test.png" UTF8String], -1, SQLITE_TRANSIENT);
  
           sqlite3_bind_blob(selectStmt, 2, [data1 bytes], [data1 length], SQLITE_TRANSIENT);
  
          if(SQLITE_DONE != sqlite3_step(selectStmt))
              NSLog(@"get: %s",sqlite3_errmsg(database));
          else
              NSLog(@"scan data added");
  
  
      }
  
      //showimage
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  
  
      if (sqlite3_open([path UTF8String], &amp;database) == SQLITE_OK)
      {
          const char *sql = "select * from imagetb";// LIMIT 0,100 ";// where status = 1 ";
          sqlite3_stmt *selectstatment;
  
          if (sqlite3_prepare_v2(database, sql, -1, &amp;selectstatment, NULL) == SQLITE_OK)
          {
              while (sqlite3_step(selectstatment) == SQLITE_ROW)
              {
  
                  NSString *getorgstrid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstatment, 0)];
                  NSLog(@"id-&gt;%@",getorgstrid);
  
                  [getid addObject:getorgstrid];
  
                  NSString *getorgstrname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstatment, 1)];
                  NSLog(@"name-&gt;%@",getorgstrname);
                  [getname addObject:getorgstrname];
  
  
                  NSData *getorgstrimage = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectstatment,2)length:sqlite3_column_bytes(selectstatment, 2)];
  
                  [getimage addObject:getorgstrimage];
  
              }
          }
          sqlite3_finalize(selectstatment);
  
      }
      //[tblorgland reloadData];
  
      UIImage *image1 = [UIImage imageWithData:[getimage lastObject]];
  
      NSLog(@"image%@",image1);
  
      [getImageview setImage:image1];
      [pool release];
  
  
  ==============================================================================
  
  - (NSString *) getDBPath
  {
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
      NSString *documentsDir = [paths objectAtIndex:0];
  
      return [documentsDir stringByAppendingPathComponent:@"image.sqlite"];
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      getid=[[NSMutableArray alloc]init];
      getname=[[NSMutableArray alloc]init];
      getimage=[[NSMutableArray alloc]init];
  
      [self downloadimage];
      [self makeDBCopyAsNeeded];
  }
  -(void)downloadimage
  {
      NSLog(@"Downloading...");
  
      UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://0.tqn.com/d/graphicssoft/1/7/g/A/5/psptubezdotcom_003.png"]]];
  
      NSLog(@"%f,%f",image.size.width,image.size.height);
  
  
      NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  
  
      NSLog(@"%@",docDir);
  
      NSLog(@"saving png");
      pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir];
      data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
  
      img=[[UIImage alloc]initWithData:data1];
  
  
  
      [data1 writeToFile:pngFilePath atomically:YES];
  
  
      NSLog(@"saving jpeg");
      NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test.jpeg",docDir];
  
      NSLog(@"jpegFilePath:%@",jpegFilePath);
      data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
  
      [data2 writeToFile:jpegFilePath atomically:YES];
  
      NSLog(@"saving image done");
  
      [image release];
  
  
  }
  - (void) makeDBCopyAsNeeded
  {
      BOOL success;
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSError *error;
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"image.sqlite"];
      success = [fileManager fileExistsAtPath:writableDBPath];
  
      NSString *dbPath = [self getDBPath];
      NSLog(@"%@",dbPath);
  
      if (success)
      {
          NSLog(@"Database Success Created");
          return;
      }
      NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"image.sqlite"];
      success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&amp;error];
      if (!success)
      {
          NSLog(@"Database:-&gt;%s",sqlite3_errmsg(database));
      }
  }
  </code></pre>