关于sqlite3 for iphone..sorry *的另外一个问题

时间:2010-03-18 08:27:26

标签: iphone sqlite uiimageview uiimage

假设我正在尝试将从照片库中选择的图像保存到数据库..以下代码是否正确?

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];
}

然后得到我使用的“对象”..

snap2playObj.snapImage = imageView.image;

我正在使用UIImageView ..我的错误消息..

-[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'

1 个答案:

答案 0 :(得分:0)

您的SQL语句与您绑定的参数不匹配。 insert语句中有三个参数:

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

但你绑定了四个参数:

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);

您需要使这些陈述匹配。