mysql 2035异常
bcb6.0 mysql5.0
void TMain::InsertPhoto(int id, TJPEGImage *image){ char * p = NULL; MYSQL_BIND bind[1]; unsigned long length; MYSQL_STMT * stmt; AnsiString sql = "Insert into `photo` (`member`,`photo`) values ("+AnsiString(id)+",?)"; stmt = mysql_stmt_init(mysql); TMemoryStream * stream = new TMemoryStream(); stream->Position = 0; image->SaveToStream(stream); unsigned long psize = stream->Size; p = new char [psize+1]; stream->Read(p,psize); if(!stmt) { ShowMessage("Stmt failed to Create!"); } if(mysql_stmt_prepare(stmt,sql.c_str(),sql.Length())) { ShowMessage(mysql_stmt_error(stmt)); } memset(bind,0,sizeof(bind)); bind[0].buffer = p; bind[0].buffer_type = MYSQL_TYPE_LONG_BLOB; bind[0].is_null = 0; if(mysql_stmt_bind_param(stmt,bind)) { ShowMessage(mysql_stmt_error(stmt)); } if(mysql_stmt_send_long_data(stmt,0,p,psize)) { ShowMessage(mysql_stmt_error(stmt)); } if(mysql_stmt_execute(stmt)) { ShowMessage(mysql_stmt_error(stmt)); } mysql_stmt_close(stmt); delete p; delete stream;}
TMemoryStream * stream = new TMemoryStream();
stream->Position = 0;//这条语句应该放到后面
image->SaveToStream(stream);
unsigned long psize = stream->Size;
p = new char [psize+1];//内存申请可能会失败,而且大图片申请失败的概率非常大,
//失败以后,经常会出现存取违规错误,导致未定义的行为发生。
stream->Read(p,psize);