wfnic
新手上路

积分 6
发帖 2
注册 2008-11-12
状态 离线
|
『楼 主』:
c访问mysql的blob字段
win2k mysql-4.0.5 devc-4.9通过
#include<windows.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<io.h>
#include<mysql.h>
int main(void)
{
unsigned long result,r,len1,i;
unsigned long *t;
MYSQL mysql;// need a instance to init
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL_FIELD *field;
char *qs,*hout,*query;
if((qs=getenv("QUERY_STRING"))!=NULL) {
strcpy(query,"select body from jdbctest.mess_body where id=");
len1=strlen(query);
for(i=0;qs[i]!='\0';i++)
query[len1+i]=qs[i];
}
else {query="select body from jdbctest.mess_body where id=14";}
mysql_init(&mysql);
mysql_real_connect(&mysql,"192.168.1.6", "wfnic", "197809",NULL,3306,NULL,0);
mysql_real_query(&mysql,query,(unsigned int)strlen(query));
res =mysql_store_result(&mysql);
if (res) row = mysql_fetch_row(res);
t = mysql_fetch_lengths(res);
field = mysql_fetch_field(res);
if (IS_BLOB(field->type)){
if (_setmode(_fileno(stdout),_O_BINARY)!=1){
r=t[0];
qs=row[0];
hout="Content-Type: image/jpeg\n\n";
fprintf(stdout,hout);
fwrite(qs,r,1,stdout);
}}else fprintf(stdout,"Content-Type: text/plain\n\n%s",row[0]);
mysql_free_result(res);
mysql_close(&mysql);
return 0;
}
|
|