关于 iphone 版 sqlite3 的另一个问题..抱歉*

发布于 2024-08-25 18:23:33 字数 1946 浏览 1 评论 0原文

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

然后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'

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2024-09-01 18:23:33

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

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

您需要使这些语句匹配。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文