loadrunner连接mysql
需要使用到的包:
链接:https://pan.baidu.com/s/1X5k0mZt1YpUdklyB8GqN9w&shfl=sharepset
提取码:s06a
步骤:
1、将如下代码放入vuser_init文件
#include"Ptt_Mysql.h"
#include"mysql.h"
#include"mysql_com.h"
#include"mysql_time.h"
#include"mysql_version.h"
#include"typelib.h"
#include"my_list.h"
#include"my_alloc.h"
#defineMYSQLSERVER"127.0.0.1"
#defineMYSQLUSERNAME"name"
#defineMYSQLPASSWORD"password"
#defineMYSQLDB"mysql"
#defineMYSQLPORT"3306"
charchQuery[1024];
intstatu=1;
intstatus=0;
MYSQL*Mconn;
charsql_query[256],sql_sub_query[50];
inti=0,res=0,conn_fail=0,conn_iter=0;
charresponse[50];// to go
vuser_init()
{
lr_load_dll("libmysql.dll");
lr_start_transaction("mysql_conn");//设置一个连接数据库的事务
/*------------------------------------------------------------------------*/
/* Initialise MySQL */
if(!(Mconn=mysql_init(NULL)))
{
lr_message("Error -1: Cannot initialize MySQL - %s",mysql_error(Mconn));
//return -1;
statu=0;
lr_end_transaction("mysql_conn",LR_FAIL);//初始化数据库失败判定事务失败
}
/*------------------------------------------------------------------------*/
do
{
/* Connect to database */
if(!mysql_real_connect(Mconn,MYSQLSERVER,MYSQLUSERNAME,MYSQLPASSWORD,MYSQLDB,atoi(MYSQLPORT),NULL,0))
{
conn_fail=-2;
conn_iter++;
sleep(100);
}
else
conn_fail=0;
}
while(conn_fail<0&&conn_iter<10);
if(conn_fail<0)
{
lr_message("Error -2: %s",mysql_error(Mconn));
//mysql_close(Mconn);
//return -2;
statu=0;
lr_end_transaction("mysql_conn",LR_FAIL);//连接数据库失败判定事务失败
}
else
{
//lr_message("MySql - Good Connection");
//mysql_close(Mconn);
statu=1;
lr_end_transaction("mysql_conn",LR_PASS);//连接数据库成功,事务通过。
}
return0;
}
2、将如下代码放入vuser_init文件
Action()
{
intcount=0;
if(statu){//成功连接数据库后才进行执行sql的操作
lr_start_transaction("mysql_select");
sprintf(chQuery,"SELECT lower(hex(id)),title from hnt_tk_ticket ");//查询论坛用户
//sprintf(chQuery,"UPDATE lr_test SET password = "%s" WHERE username='tom'",lr_eval_string ("{pwd}"));
/*
sprintf(chQuery,"update pre_ucenter_members set username='dd' where username='qq' ");//更改用户名qq为dd
sprintf(chQuery,"delete from pre_ucenter_members ORDER BY uid desc LIMIT 1");//删除最后添加的一条数据
*/
status=lr_mysql_query(Mconn,chQuery);
count=mysql_affected_rows(Mconn);
lr_output_message("xxxxxxxxxxxxx:%s",lr_eval_string("count"));
for(i=0;i<count;i++){//输出查询的值
lr_save_string(row[0][i].cell,"test");
lr_save_string(row[1][i].cell,"test2");
lr_output_message("xxxxxxxxxxxxx:%s",lr_eval_string("{test}"));
lr_output_message("xxxxxxxxxxxxx:%s",lr_eval_string("{test2}"));
}
if(status!=0){//判断sql是否执行成功
lr_end_transaction("mysql_select",LR_FAIL);
}else{
lr_end_transaction("mysql_select",LR_PASS);
}
// lr_start_transaction("mysql_insert");
// sprintf(chQuery,"insert into oa_news(id, lb, title, nr, attachment_id, attachment_name, pic_name, fbsj, fbbm, fbr, vcount) values(9,9,'qq@qq.com','test','','','teste','',6,'test',0)");//插入数据
// status = lr_mysql_query(Mconn, chQuery);
// if(status!=0){ //判断sql是否执行成功
// lr_end_transaction("mysql_insert",LR_FAIL);
// }else{
// lr_end_transaction("mysql_insert",LR_PASS);
// }
}
return0;
}
3、将如下代码防止vuser_end文件
vuser_end()
{
lr_mysql_disconnect(Mconn);//关闭数据库连接
return0;
}
不过这里会存在中文编码问题,如果你插入的数据有中文,需要加上mysql_query(db_connection,lr_eval_string("SET NAMES gbk"));
我连接数据库主要是想把数据写入文件中所以还在action中添加了如下代码。
char*filename="C:\\loadrunner\\mysql.dat";
longfile;
count=mysql_affected_rows(Mconn);
if((file=fopen(filename,"w+"))==NULL){
lr_output_message("Unable to create %s",filename);
return-1;
}
//获取数据,然后写入到文件中
for(i=0;i<count;i++)
{
lr_save_string(row[0][i].cell,"id");
lr_save_string(row[1][i].cell,"caseid");
lr_save_string(row[2][i].cell,"title");
// lr_output_message("xxxxxxxxxxxxx:%s",lr_eval_string("{id}"));
// lr_output_message(":%s",lr_eval_string("{caseid}"));
// lr_output_message(":%s",lr_eval_string("{title}"));
strcat(row[0][i].cell," ");//用空格分隔
strcat(row[0][i].cell,row[1][i].cell);//连接
strcat(row[0][i].cell," ");//用空格分隔
strcat(row[0][i].cell,row[2][i].cell);//连接
strcat(row[0][i].cell,"\n");//添加回车换行标识
fwrite(row[0][i].cell,strlen(row[0][i].cell),1,file);//把一组信息写入参数文件
}
fclose(file);//关闭文件
感谢网络上各位大神的分享,从各位大神哪里学习loadrunner。