mysql预处理速度对比_mysql直接执行和预处理执行的效率比较 | 学步园

{

if(mysql_stmt_prepare(stmt,"INSERT test(col1,col2,col3,col4) values(?,?,?,?)", strlen("INSERT test(col1,col2,col3,col4) values(?,?,?,?)")))

{

fprintf(stderr,"mysql_stmt_prepare(), insert failed\r\n");

fprintf(stderr,"%s\r\n", mysql_stmt_error(stmt));returnfalse;

}//测试mysql_stmt_field_count()作用//printf("mysql_stmt_field_count return value[%d]\r\n",mysql_stmt_field_count(stmt));MYSQL_BIND    bind[4];

unsignedlonglength[4];

my_bool       is_null[4];

memset(bind,0,sizeof(bind));

memset(length,0,sizeof(length));

memset(is_null,0,sizeof(is_null));charvarchar_var[STRING_SIZE]="hello yg";charchar_var[STRING_SIZE]="hello dl";structst_stud st;

st.id=1;

strncpy(st.name,"shine.yang",16);

st.score=10.0f;

time_t cur_t=time(NULL);structtm cur_tm;

localtime_r(&cur_t,&cur_tm);

MYSQL_TIME    ts;

ts.year=cur_tm.tm_year+1900, ts.month=cur_tm.tm_mon+1, ts.day=cur_tm.tm_mday,ts.hour=cur_tm.tm_hour, ts.minute=cur_tm.tm_min, ts.second=cur_tm.tm_sec;

bind[0].buffer_type=MYSQL_TYPE_STRING;

bind[0].buffer=varchar_var;

bind[0].buffer_length=STRING_SIZE;

length[0]=strlen(varchar_var);

bind[0].length=&length[0];

bind[0].is_null=&is_null[0];

bind[1].buffer_type=MYSQL_TYPE_STRING;

bind[1].buffer=char_var;

bind[1].buffer_length=STRING_SIZE;

length[1]=strlen(char_var);

bind[1].length=&length[1];

bind[1].is_null=&is_null[1];

bind[2].buffer_type=MYSQL_TYPE_BLOB;

bind[2].buffer=(char*)&st;

bind[2].buffer_length=sizeof(st);

length[2]=sizeof(st);

bind[2].length=&length[2];

bind[2].is_null=&is_null[2];

bind[3].buffer_type=MYSQL_TYPE_TIMESTAMP;

bind[3].buffer=(char*)&ts;

bind[3].buffer_length=sizeof(ts);

length[3]=sizeof(ts);

bind[3].length=&length[3];

bind[3].is_null=&is_null[3];

dword time1=get_time();for(inti=0; i

{if( mysql_stmt_bind_param(stmt,bind) )

{

fprintf(stderr,"mysql_stmt_bind_param failed,err = %s\r\n",mysql_stmt_error(stmt));returnfalse;

}if( mysql_stmt_execute(stmt) )

{

fprintf(stderr,"mysql_stmt_excute failed,err = %s\r\n",mysql_stmt_error(stmt));returnfalse;

}//printf("Total effect rows [%d]\r\n",(int)mysql_stmt_affected_rows(stmt));}

dword time2=get_time();

fprintf(stdout,"stmt insert %d items,during time[%d]\r\n",INSERT_ITEM_CNT,time2-time1);returntrue;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值