title: sqlite3 blob problem
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4215505/sqlite3-insert-and-read-blob-data-in-database">sqlite3 insert and read BLOB data in database</a>  </p>
  </blockquote>
  
  
  
  <p>I try to read-write BLOB data to database. But when I try to read blob data - the size of it is 0. And all my fields data moved. here is the code:</p>
  
  <p>Please, assist me, what I'm doing wrong?? Thanx!</p>
  
  <pre><code>-(int) addUser: (MyData *) user table: (NSString *) tableName
  </code></pre>
  
  <p>{</p>
  
  <pre><code>sqlite3_stmt *updStmt =nil; 
  
  const char *sql = "INSERT INTO Users (Name, Surname, Email, Phone, Image) VALUES (?, ?, ?, ?, ?);";
  int res = SQLITE_ERROR;
  res = sqlite3_prepare_v2(database, sql, -1, &amp;updStmt, NULL);
  
  if(res!= SQLITE_OK)
  {
      NSLog(@"Error while creating update statement:%@", sqlite3_errmsg(database));
      return res;
  }
  
  res = sqlite3_bind_text(updStmt, 1, [user.name UTF8String], -1, SQLITE_TRANSIENT);
  res = sqlite3_bind_text(updStmt, 2, [user.surname UTF8String], -1, SQLITE_TRANSIENT);  
  res = sqlite3_bind_text(updStmt, 3, [user.email UTF8String], -1, SQLITE_TRANSIENT);
  res = sqlite3_bind_text(updStmt, 4, [user.telephone UTF8String], -1, SQLITE_TRANSIENT); 
  
  NSData *imageData = [NSData dataWithData: UIImagePNGRepresentation(user.image)];
  res = sqlite3_bind_blob(updStmt, 5, imageData, -1, SQLITE_BLOB);
  
  if((res = sqlite3_step(updStmt)) != SQLITE_DONE)
  {
      NSLog(@"Error while updating: %@", sqlite3_errmsg(database));
      sqlite3_reset(updStmt);
      return res;
  } 
  
  sqlite3_reset(updStmt);
  
  return res;
  </code></pre>
  
  <p>}</p>
  
  <p>and here how can I read this fields:</p>
  
  <pre><code>-(MyData *) createUserWithDetailsByID: (int) uID
  {
      NSString *query = [NSString stringWithFormat: @SELECT_USER_ALLDATA_BY_ID, uID];
      MyData *user = nil;
  
      sqlite3_stmt *statement;
      int res = SQLITE_ERROR;
      res = sqlite3_prepare_v2 (database, [query UTF8String], -1, &amp;statement, nil);
  
      if (res == SQLITE_OK)
      {
          if (sqlite3_step(statement) == SQLITE_ROW)
          {
              //if there are results in row
              char *buf = NULL;
              user = [[MyData alloc] init];
  
              int num = sqlite3_column_int(statement, 0);
              user.uID = num;
  
              buf = (char *)sqlite3_column_text(statement, 1);
              if (buf)
                  user.name = [NSString stringWithFormat: @"%s", buf];
              else
                  user.name = @"";    
  
              buf = (char *)sqlite3_column_text(statement, 2);
              if (buf)
                  user.surname = [NSString stringWithFormat: @"%s", buf];
              else
                  user.surname = @"";
  
              buf = (char *)sqlite3_column_text(statement, 3);
              if (buf)
                  user.email = [NSString stringWithFormat: @"%s", buf];
              else
                  user.email = @"";
  
              buf = (char *)sqlite3_column_text(statement, 4);
              if (buf)
                  user.telephone = [NSString stringWithFormat: @"%s", buf];
              else
                  user.telephone = @"";
  
              int len = 0;
              len = sqlite3_column_bytes(statement, 5);
  
              NSData *imgData = [[NSData alloc] initWithBytes: sqlite3_column_blob(statement, 5) length: len];           
  
              UIImage *i;
              [i initWithData:imgData];
  
          }
      }
      sqlite3_finalize(statement);
  
      return user; 
  }
  </code></pre>