views:

238

answers:

1

let's say i am trying to save an image selected from photo library to database..is the following code correct?

Snap.m

- (void) addSnap {
  if(addStmt == nil) {
  const char *sql = "insert into Snap(snapTitle, snapDesc, snapImage) Values(?, ?, ?)";
  if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
    NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
  }
  sqlite3_bind_text(addStmt, 1, [snapTitle UTF8String], -1, SQLITE_TRANSIENT);//bind titleSnap to insert statement
  sqlite3_bind_text(addStmt, 2, [snapDescription UTF8String], -2, SQLITE_TRANSIENT);
  sqlite3_bind_int(addStmt, 3, snapID);

  NSData *imgData = UIImagePNGRepresentation(self.snapImage);
  int returnValue = -1;
  if(self.snapImage != nil)
    returnValue = sqlite3_bind_blob(addStmt, 3, [imgData bytes], [imgData length], SQLITE_TRANSIENT);
  else
    returnValue = sqlite3_bind_blob(addStmt, 3, nil, -1, NULL);

  sqlite3_bind_int(addStmt, 4, snapID);

  if(returnValue != SQLITE_OK)
    NSLog(@"Not OK!!!");



  if(SQLITE_DONE != sqlite3_step(addStmt))//execute step statement if it return SQLITE_DONE
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
  else
//sqlite3_last_insert_rowid
    snapID = sqlite3_last_insert_rowid(database);//get primary key for the row which was inserted

//reset add statement
   sqlite3_reset(addStmt);
}

- (void)setSnapImage:(UIImageView *)theSnapImage {
  self.isDirty = YES;
  [snapImage release];
  snapImage = [theSnapImage copy];
}

then to get the "object" i use..

snap2playObj.snapImage = imageView.image;

i am using UIImageView by the way..my error message..

-[UIImage copyWithZone:]: unrecognized selector sent to instance 0xd74750
 2010-03-18 16:22:27.808 Snap2Play[68317:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIImage copyWithZone:]: unrecognized selector sent to instance 0xd74750'
A: 

Your SQL statement does not match the parameters your are binding. You have three parameters in your insert statement:

const char *sql = "insert into Snap(snapTitle, snapDesc, snapImage) Values(?, ?, ?)";

but you bind four parameters:

sqlite3_bind_text(addStmt, 1, [snapTitle UTF8String], -1, SQLITE_TRANSIENT);//bind titleSnap to insert statement
sqlite3_bind_text(addStmt, 2, [snapDescription UTF8String], -2, SQLITE_TRANSIENT);
sqlite3_bind_int(addStmt, 3, snapID);

sqlite3_bind_int(addStmt, 4, snapID);

You need to make these statements match.

kharrison
how do i retrieve the image that was save?
summer
did you not already post that question here:http://stackoverflow.com/questions/2445889/sqlite3-problem-need-help-urgently-help/2446195#2446195Would be good if you started accepting some of the answers
kharrison