1.首先安装mysql数据驱动,qtp在windows系统下操作连接mysql,所以下载mysql-connector-odbc- 5.1.8-win32.msi
下载地址:http://download.csdn.net/detail/tcybb/6688599
2.安装mysql驱动 ,默认安装即可。
3. 添加默认数
控制面板->管理工具->数据源ODBC->系统DNS->添加->在“创建数据源”对话框中选择
“Mysql ODBC 5.1 Driver”->完成->进入连接对话框->填写完后点击“test”,看看是否成功,成功后配置完成。
注意:64位win7的要到C:WindowsSysWOW64下面执行odbcad32.exe添加配置
4.qtp脚本实现连接mysql数据库
第一种写的方式
Dim Conn
Set Conn=CreateObject("ADODB.Connection")
Const ConnectionString="DSN=test;DATABASE=han;PWD=root;PORT=3306;SERVER=localhost;UID=root"
'Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=test;PWD=root;PORT=3306;SERVER=localhost;UID=root"
Conn.Open ConnectionString
If Conn.State<>0Then
Reporter.ReportEvent micPass,"testing","连接数据库成功"
else
Reporter.ReportEvent micFail,"testing","连接数据库失败"
End If
Set Conn=CreateObject("ADODB.Connection")
Const ConnectionString="DSN=test;DATABASE=han;PWD=root;PORT=3306;SERVER=localhost;UID=root"
'Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=test;PWD=root;PORT=3306;SERVER=localhost;UID=root"
Conn.Open ConnectionString
If Conn.State<>0Then
Reporter.ReportEvent micPass,"testing","连接数据库成功"
else
Reporter.ReportEvent micFail,"testing","连接数据库失败"
End If
注意:其中DSN=数据源名;UID=用户名;PWD=用户密码
第二种写的方式:
Dim Conn
Set Conn=CreateObject("ADODB.Connection")
'Const ConnectionString="DSN=test;DATABASE=han;PWD=root;PORT=3306;SERVER=localhost;UID=root"
Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=test;PWD=root;PORT=3306;SERVER=localhost;UID=root"
Conn.Open ConnectionString
If Conn.State<>0Then
Reporter.ReportEvent micPass,"testing","连接数据库成功"
else
Reporter.ReportEvent micFail,"testing","连接数据库失败"
End If
Set Conn=CreateObject("ADODB.Connection")
'Const ConnectionString="DSN=test;DATABASE=han;PWD=root;PORT=3306;SERVER=localhost;UID=root"
Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=test;PWD=root;PORT=3306;SERVER=localhost;UID=root"
Conn.Open ConnectionString
If Conn.State<>0Then
Reporter.ReportEvent micPass,"testing","连接数据库成功"
else
Reporter.ReportEvent micFail,"testing","连接数据库失败"
End If
5.qtp脚本实现查询数据
Dim Conn,str,sql,i,sum
Set Conn=CreateObject("ADODB.Connection")'创建数据库实例
Const ConnectionString="DSN=test;DATABASE=han;PWD=root;PORT=3306;SERVER=localhost;UID=root"
'Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=han;PWD=root;PORT=3306;SERVER=localhost;UID=root"
Conn.Open ConnectionString
If Conn.State<>0Then
Reporter.ReportEvent micPass,"testing","连接数据库成功"
else
Reporter.ReportEvent micFail,"testing","连接数据库失败"
End If
Set str=CreateObject("ADODB.Recordset")'创建数据集实例
'查询数据库
sql="Select * from user1"
str.Open sql,conn,1,1'1,1表示只读;1,3表示插入数据;2,3表示修改数据
str.MoveFirst'使游标指向第一个记录
sum=""
WhileNot str.EOF
'msgbox str.Fields("username")
For i=0to str.Fields.Count-1'str.Fields.Count表示字段个数
sum=sum & str(i) &""'把整个记录显示出来
Next
Print sum & vbCRLF'打印所有查询的记录
Set Conn=CreateObject("ADODB.Connection")'创建数据库实例
Const ConnectionString="DSN=test;DATABASE=han;PWD=root;PORT=3306;SERVER=localhost;UID=root"
'Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=han;PWD=root;PORT=3306;SERVER=localhost;UID=root"
Conn.Open ConnectionString
If Conn.State<>0Then
Reporter.ReportEvent micPass,"testing","连接数据库成功"
else
Reporter.ReportEvent micFail,"testing","连接数据库失败"
End If
Set str=CreateObject("ADODB.Recordset")'创建数据集实例
'查询数据库
sql="Select * from user1"
str.Open sql,conn,1,1'1,1表示只读;1,3表示插入数据;2,3表示修改数据
str.MoveFirst'使游标指向第一个记录
sum=""
WhileNot str.EOF
'msgbox str.Fields("username")
For i=0to str.Fields.Count-1'str.Fields.Count表示字段个数
sum=sum & str(i) &""'把整个记录显示出来
Next
Print sum & vbCRLF'打印所有查询的记录
sum="" '清零
str.MoveNext'使游标进入下一个
Wend
str.Close '关闭数据集实例
Set str=Nothing
Conn.Close '关闭数据库实例
Set Conn=Nothing
Wend
str.Close '关闭数据集实例
Set str=Nothing
Conn.Close '关闭数据库实例
Set Conn=Nothing
1.查询数据库中是否存在该值
sql="select account from user where account='"+ userName +"'"
str.Open sql,conn,1,1
If str.RecordCount<>0 then
msgbox("存在")
else
msgbox("不存在")
end if
str.Open sql,conn,1,1
If str.RecordCount<>0 then
msgbox("存在")
else
msgbox("不存在")
end if
2.打印出sql查询出来的值
While not str.EOF
For j=0 to str.Fields.Count-1
sum=sum &str(j) &" "
Next
print char & sum & vbCRLF
sum=""
str.MoveNext
Wend
For j=0 to str.Fields.Count-1
sum=sum &str(j) &" "
Next
print char & sum & vbCRLF
sum=""
str.MoveNext
Wend