loadrunner12:常用函数汇总说明之Database Functions参数函数,操作mysql数据库
2023-09-14 08:59:26 时间
环境准备:
1、准备好测试用的Mysql数据库。
2、下载并安装mysql odbc x32:https://dev.mysql.com/downloads/connector/odbc/
3、配置数据源(应该配置32位数据源)
遇到的问题:
1、“DB Connection failed {"ERROR [IM002] [Microsoft] [ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序”,主要原因时LR12为32位的,无法读取64位的数据源,将64位数据源改为32位即可。
解决办法:参考https://jingyan.baidu.com/article/6d704a13407c4128db51ca2d.html
安装32位的mysql-connector-odbc-8.0.25-win32,并重启电脑,查看之前配置的数据源显示支持了32位的。
根据使用手册内容可见,LR支持的关于Database Functions包括:
lr_db_connect | Connects to a database. |
lr_db_dataset_action | Performs an action on a dataset. |
lr_db_disconnect | Disconnects from a database. |
lr_db_executeSQLStatement | Submits an SQL statement to a database. |
lr_db_getValue | Retrieves a value from a dataset. |
以下举例使用的数据为本地MySql数据库testdb中的student表:
脚本展示:
1、查询student表中数据
ConnMysql() { //定义两个变量 int i=1,NumRows; //创建数据库连接 lr_db_connect("StepName=conMySql", "ConnectionString=Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Database=testdb;Trusted_Connection=False;User=root;Password=mysql", "ConnectionName=mysqlData", "ConnectionType=ODBC", LAST ); //查询结果保存在Students数据集中,并返回查询记录数据总条数,存入NumRows中 NumRows=lr_db_executeSQLStatement("StepName=findStudent", "ConnectionName=mysqlData", "SQLStatement=select * from student;", "DatasetName=StudentsSet", LAST ); lr_output_message("The query returned %d rows.", NumRows); //循环打印所有记录的id、name、birthday,score。 while(i<NumRows){ lr_db_getvalue("StepName=showStudentID", "DatasetName=StudentsSet", "Column=id", "Row=next", "OutParam=studentId", LAST); lr_db_getvalue("StepName=showStudentName", "DatasetName=StudentsSet", "Column=name", "Row=current", "OutParam=studentName", LAST); lr_db_getvalue("StepName=showStudentBirthday", "DatasetName=StudentsSet", "Column=birthday", "Row=current", "OutParam=studentBirthday", LAST); lr_db_getvalue("StepName=showStudentScore", "DatasetName=StudentsSet", "Column=score", "Row=current", "OutParam=studentScore", LAST); lr_output_message(lr_eval_string("{studentId}")); lr_output_message(lr_eval_string("{studentName}")); lr_output_message(lr_eval_string("{studentBirthday}")); lr_output_message(lr_eval_string("{studentScore}")); i=i+1; } //关闭数据库的链接 lr_db_disconnect("StepName=disconnectMysql", "ConnectionName=mysqlData", LAST); return 0; }
2、更新student表中的数据
UpdateMysql() { int NumRows=0; //创建数据库连接 lr_db_connect("StepName=conMySql", "ConnectionString=Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Database=testdb;Trusted_Connection=False;User=root;Password=mysql", "ConnectionName=mysqlData", "ConnectionType=ODBC", LAST ); //更新student表中学生“张三”的分数 lr_db_executeSQLStatement("StepName=updateScore", "ConnectionName=mysqlData", "SQLStatement=UPDATE student SET score=33 WHERE NAME=\"张三\";", "DatasetName=StudentsSet", LAST ); //查询结果保存在Students数据集中,并返回查询记录数据总条数,存入NumRows中 NumRows=lr_db_executeSQLStatement("StepName=findStudent", "ConnectionName=mysqlData", "SQLStatement=select * from student where NAME=\"张三\";", "DatasetName=StudentsSet", LAST ); lr_output_message("The query returned %d rows.", NumRows); //获取查询到的数据集StudentsSet中的分数 lr_db_getvalue("StepName=showScore", "DatasetName=StudentsSet", "Column=score", "Row=next", "OutParam=studentScore", LAST); lr_output_message("学生“张三”的分数为:%s", lr_eval_string("{studentScore}")); //关闭数据库的链接 lr_db_disconnect("StepName=disconnectMysql", "ConnectionName=mysqlData", LAST); return 0; }
3、删除一条学生记录
DeleteMysql() { int NumRows=0; //创建数据库连接 lr_db_connect("StepName=conMySql", "ConnectionString=Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Database=testdb;Trusted_Connection=False;User=root;Password=mysql", "ConnectionName=mysqlData", "ConnectionType=ODBC", LAST ); //删除student表中学生“七七”的记录 lr_db_executeSQLStatement("StepName=DeleteScore", "ConnectionName=mysqlData", "SQLStatement=DELETE FROM student WHERE NAME=\"七七\";", "DatasetName=StudentsSet", LAST ); //查询结果保存在Students数据集中,并返回查询记录数据总条数,存入NumRows中 NumRows=lr_db_executeSQLStatement("StepName=findStudent", "ConnectionName=mysqlData", "SQLStatement=select * from student where NAME=\"七七\";", "DatasetName=StudentsSet", LAST ); lr_output_message("The query returned %d rows.", NumRows); if(NumRows==0){ lr_output_message("删除成功!"); }else{ lr_error_message("删除失败"); } //关闭数据库的链接 lr_db_disconnect("StepName=disconnectMysql", "ConnectionName=mysqlData", LAST); return 0; }
4、插入一条学生记录
InsertMysql() { //创建数据库连接 lr_db_connect("StepName=conMySql", "ConnectionString=Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Database=testdb;Trusted_Connection=False;User=root;Password=mysql", "ConnectionName=mysqlData", "ConnectionType=ODBC", LAST ); //插入一条记录,并保存在Students数据集中 lr_db_executeSQLStatement("StepName=updateScore", "ConnectionName=mysqlData", "SQLStatement=INSERT INTO student VALUES(9,\"小酒\",NOW(),99);", "DatasetName=StudentsSet", LAST ); //查询插入的记录,并保存在Students数据集中 lr_db_executeSQLStatement("StepName=findStudent", "ConnectionName=mysqlData", "SQLStatement=select * from student where NAME=\"小酒\";", "DatasetName=StudentsSet", LAST ); lr_db_getvalue("StepName=showID", "DatasetName=StudentsSet", "Column=id", "Row=next", "OutParam=studentID", LAST); lr_db_getvalue("StepName=showName", "DatasetName=StudentsSet", "Column=name", "Row=current", "OutParam=studentName", LAST); lr_db_getvalue("StepName=showBirth", "DatasetName=StudentsSet", "Column=birthday", "Row=current", "OutParam=studentBirth", LAST); lr_db_getvalue("StepName=showScore", "DatasetName=StudentsSet", "Column=score", "Row=current", "OutParam=studentScore", LAST); lr_output_message(lr_eval_string("{studentId}")); lr_output_message(lr_eval_string("{studentName}")); lr_output_message(lr_eval_string("{studentBirth}")); lr_output_message(lr_eval_string("{studentScore}")); //关闭数据库的链接 lr_db_disconnect("StepName=disconnectMysql", "ConnectionName=mysqlData", LAST); return 0; }
5、查询所有记录、重置游标、释放数据集内存:( lr_db_dataset_action())
QueryDataSet() { //创建数据库连接 lr_db_connect("StepName=conMySql", "ConnectionString=Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Database=testdb;Trusted_Connection=False;User=root;Password=mysql", "ConnectionName=mysqlData", "ConnectionType=ODBC", LAST ); //查询结果保存在Students数据集中,并返回查询记录数据总条数,存入NumRows中 lr_db_executeSQLStatement("StepName=findStudent", "ConnectionName=mysqlData", "SQLStatement=select * from student;", "DatasetName=StudentsSet", LAST ); /* RESET: Set the cursor to the first record of the dataset. REMOVE: Releases the memory allocated for the dataset. PRINT: Prints the contents of the entire dataset to the Replay Log and other test report summaries. */ lr_db_dataset_action("StepName=PrintDataset", "DatasetName=StudentsSet", "Action=PRINT", //RESET、REMOVE、PRINT LAST ); lr_db_getvalue("StepName=showName", "DatasetName=StudentsSet", "Column=name", "Row=next", "OutParam=studentName", LAST); lr_output_message("第一条记录的学生名称为:%s",lr_eval_string("{studentName}")); lr_db_getvalue("StepName=showName", "DatasetName=StudentsSet", "Column=name", "Row=next", "OutParam=studentName", LAST); lr_output_message("第二条记录的学生名称为:%s",lr_eval_string("{studentName}")); //将数据库游标重置到数据集的第一条记录 lr_db_dataset_action("StepName=PrintDataset", "DatasetName=StudentsSet", "Action=RESET", //RESET、REMOVE、PRINT LAST ); lr_db_getvalue("StepName=showName", "DatasetName=StudentsSet", "Column=name", "Row=next", //若无以上RESET操作,此时查出的记录应为第三条记录的学生名称 "OutParam=studentName", LAST); lr_output_message("游标重置后查询的记录为第一条记录的学生名称为:%s",lr_eval_string("{studentName}")); //释放分配给数据集的内存。再次查询数据集的数据是将会报错“数据集未定义”,即{"Undefined dataset [Dataset name=StudentsSet]"} lr_db_dataset_action("StepName=PrintDataset", "DatasetName=StudentsSet", "Action=REMOVE", //RESET、REMOVE、PRINT LAST ); lr_db_getvalue("StepName=showName", "DatasetName=StudentsSet", "Column=name", "Row=next", "OutParam=studentName", LAST); //关闭数据库的链接 lr_db_disconnect("StepName=disconnectMysql", "ConnectionName=mysqlData", LAST); return 0; }
遗留问题:
lr_db_getvalue()读取单个学生的中文姓名时,打印出来中文名字只打印一半,中文被截断了,但是读取英文不存在该问题(如下图所示),目前尚未定位出原因及解决办法,欢迎评论去讨论~~~
相关文章
- MySQL命令提示符:用技巧实现数据库功能(mysql命令提示符)
- 解决MySQL乱码:5步轻松搞定(mysql乱码解决方法)
- 查询MySQL表的大小:简单几步(mysql查询表大小)
- MySQL数据库深度探索:遍历之旅(mysql数据库遍历)
- 使用Python操作MySQL数据库快速上手(python访问mysql数据库)
- MySQL安装完成:最后一步(mysql安装最后一步)
- 功能MySQL数据库的安全加密功能(mysql自带加密)
- MySQL死锁:知晓真相的原因(mysql死锁的原因)
- MySQL中操作字符串和数组示范(mysql字符串数组)
- 从 HTML 页面访问 MySQL 数据库.(html访问mysql)
- MySQL配置与安装步骤指南(mysql配置安装)
- MySQL数据库认证:保护你的资料库安全(mysql数据库认证)
- 比较:Hive与MySQL的不同之处(hive和mysql的区别)
- MySQL中的二进制数据存储方法(mysql二进制数据)
- 如何查看MySQL数据库编码?(查看mysql数据库编码)
- MySQL实战技巧:多表连接查询简介(mysql多表连接查询)
- MySQL数据库常见问题解决方案(mysql数据库常见问题)
- MySQL数据库导出:快速操作指南(mysql数据库如何导出)
- MySQL:开启征途,实现数据库管理新突破。(mysql征途)
- 比较SQL Server和MySQL:你应该知道的25个区别(sqlserver和mysql的区别)
- 如何查看 MySQL 数据库中所有字段?(mysql查看所有字段)
- 探究MySQL数据库中某天的数据特征(mysql某天的数据)
- 脚本使用Bat命令快速执行MySQL脚本(bat命令执行mysql)
- 快速掌握常用Mysql语句,让你的MySQL更强大(常用mysql语句)
- 妙用MySQL数据库:用图片存放记忆(图片存入mysql数据库)
- 深入探究MySQL中的主键pk作用与使用方法(mysql中 pk)
- MySQL中的h主机地址连接数据库必备常识(mysql中-h主机地址)
- MySQL PDO数据库操作的基础和经验(ci mysql pdo)
- 用cmd操作MySQL指南(cmd 连mysql)
- MySQL通过CMD登录的自动化操作方法(cmd登陆mysql】)
- net编程mySQL数据库从入门到精通(.net 编程mysql)
- MySQL删除数据库的语句(mysql中删除库的语句)
- MySQL三范了解完整性约束数据完整性和数据访问控制(mysql三范)
- 如何在MySQL中不建立主键来优化数据库(mysql 不建立主键)
- MySQL数据库字段不允许为空值(mysql不允许null)
- MySQL的双重分组查询(mysql 两层分组查询)
- 了解MySQL中不等于符号的正确使用方法和转义技巧(mysql 不等于 转译)
- 提高效率MySQL递归操作非函数实现(mysql不用函数递归)