title: Saving UIImage as Blob SQLite
body: <p>I have the following code that pushes information to a database. It gives a successful insert but the "canvas" column (the one that contains the image blob) is NULL. I have modeled my use of sqlite3_prepare_v2 and sqlite3_bind_blob after other answers that I've been looking at on StackOverflow.</p>
  
  <pre><code>-(void) sendMoment: (Album *) alb moment:(Moment *) m
  {
      @try {
          NSFileManager *fileMgr = [NSFileManager defaultManager];
  
          // First, test for existence of writable file:
          NSError *error;
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"pictures.sqlite"];
  
          BOOL success = [fileMgr fileExistsAtPath:writableDBPath];
          if (!success){
              // The writable database does not exist, so copy the default to the appropriate location.
              NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pictures.sqlite"];
              success = [fileMgr copyItemAtPath:defaultDBPath toPath:writableDBPath error:&amp;error];
              if (!success) {
                  NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
              }
          }
  
          NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
          [dateFormat setDateFormat:@"yyy-MM-dd HH:mm:ss"];
          NSString *dateString = [dateFormat stringFromDate: m.timestamp];
  
          //add passed-in moment to the given album
          sqlite3_stmt *sqlStatement;
          NSData *imageData = UIImageJPEGRepresentation([m.moment firstObject], 1.0);
  
  
          NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM MomentTbl WHERE user = \"t-amkruz\""];
  
          NSString *momentInsertSQL = [NSString stringWithFormat:@"INSERT INTO MomentTbl (momentID, albumID, title, timestamp, author, latitude, longitude, canvas) VALUES (NULL, %d, '%@', '%@', '%@', '%@', '%@', ?)", alb.ID, alb.title, dateString, @"user", m.latitude, m.longitude];
          const char *query_stmt = [momentInsertSQL UTF8String];
          const char *dbPath = [writableDBPath UTF8String];
  
          int result = sqlite3_open(dbPath, &amp;db);
  
          if (SQLITE_OK == result) {
              char *errInfo = nil;
  
              sqlite3_prepare_v2(db, query_stmt, -1, &amp;sqlStatement, NULL);
  
              sqlite3_bind_blob(sqlStatement, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
  
              result = sqlite3_exec(db, query_stmt, nil, nil, &amp;errInfo);
              if (SQLITE_OK == result) {
                  NSLog(@"Sucessful insert");
              } else {
                  NSLog(@"Insert failed");
              }
              sqlite3_close(db);
          }
  
          else {
              NSLog(@"Could not open database");
          }
  
      }
      @catch (NSException *exception) {
          NSLog(@"An exception occured: %@", [exception reason]);
      }
  }
  </code></pre>
  
  <p>Again, the only part that I have any problems with is the last column; values from every other column insert correctly. I've verified that  <code>imageData</code> is non-<code>nil</code>. The prepare_v2 statement and the bind_blob statement both return 0 when I check them with NSLog. I would really appreciate help!</p>