QTP连接MySQL (转载)

1.首先安装mysql数据驱动,qtp在windows系统下操作连接mysql,所以下载mysql-connector-odbc-   5.1.8-win32.msi

   下载地址:http://mysql.mirrors.pair.com/Downloads/Connector-ODBC/5.1/mysqlconnector-odbc-5.1.8-win32.msi51Testing软件测试网]2Uz7G#@"W El8q

2.安装mysql驱动 ,默认安装即可。

s$N8rPm+g0

3. 添加默认数据源51Testing软件测试网n3e6SfckQ

   控制面板->管理工具->数据源ODBC->系统DNS->添加->在“创建数据源”对话框中选择

\,{A~1Ck0

  “Mysql   ODBC 5.1 Driver”->完成->进入连接对话框->填写完后点击“test”,看看是否成功,成功后配置完成。

a@#N(T1^PU0

  51Testing软件测试网oe-Y*v:uT9Ke

4.qtp脚本实现连接mysql数据库

8bka(R X'oS m"O0

 如果操作了第3步骤,添加了mysql默认数据源,脚本可以写成:51Testing软件测试网f ?k/Y;FA

DimConn
CP!Z6ud0SetConn=CreateObject("ADODB.Connection")
bC{OD] b BJ0ConstConnectionString="DSN=mysql_Mail;DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"51Testing软件测试网_LR6JYLE
'Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"51Testing软件测试网/?"] ?NqJ~S
Conn.Open ConnectionString
,V+x,LY)O9y0IfConn.State<>0Then51Testing软件测试网a y4c.}GE1`-Fx:K
Reporter.ReportEvent micPass,"testing","连接数据库成功"
jo&lHW!Y2S M9@0else
F1U!L,[(O*Q0Reporter.ReportEvent micFail,"testing","连接数据库失败"51Testing软件测试网w L.{e3D m:i'\
EndIf

如果没有进行第3个步骤,没有添加默认数据源,可以用普通方式连接51Testing软件测试网3c!A5P(Gh/J8~s~O(Q

DimConn
1yO"th:z;[ B0SetConn=CreateObject("ADODB.Connection")51Testing软件测试网VGt3`^-`$h b g-s
'Const ConnectionString="DSN=mysql_Mail;DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"51Testing软件测试网;V.fM!Nj:__
ConstConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"51Testing软件测试网6{L"Bw%x h$l
Conn.Open ConnectionString
`PMqp nX}H0d0N0IfConn.State<>0Then51Testing软件测试网,f^$C'hPlVy
Reporter.ReportEvent micPass,"testing","连接数据库成功"51Testing软件测试网L4V3xpnZ3GY
else51Testing软件测试网r,Eb"l'n"o@n:y:A
Reporter.ReportEvent micFail,"testing","连接数据库失败"51Testing软件测试网3a,@r5Cg
EndIf

5.qtp脚本实现查询数据库

/B9T4ztk c9KP3[B0
DimConn,str,sql,i,sum51Testing软件测试网0?:z;c\%P;W1G
SetConn=CreateObject("ADODB.Connection")'创建数据库实例
Y3Yc+@/W(L:E0ConstConnectionString="DSN=mysql_Mail;DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"51Testing软件测试网c @"\/l$Tf H
'Const ConnectionString="Driver={Mysql ODBC 5.1 Driver};DATABASE=extmail;PWD=123456;PORT=3306;SERVER=192.168.2.52;UID=root"
9g-q.z:k'\0Conn.Open ConnectionString
?9Ys!X X!qY|0IfConn.State<>0Then51Testing软件测试网$iR@M.gf)i gj(a
Reporter.ReportEvent micPass,"testing","连接数据库成功"51Testing软件测试网?*dUl(F}2^8U fI$t
else
x_$q\j0Reporter.ReportEvent micFail,"testing","连接数据库失败"51Testing软件测试网!E\lJqS$|
EndIf

%^!b1fm#pu0Setstr=CreateObject("ADODB.Recordset")'创建数据集实例51Testing软件测试网)J4Y8lb5puSg;G

/u5c.l*I"f$W/[9xX0'查询数据库51Testing软件测试网M&N1jy\Mg
sql="Select * from manager where type='postmaster'"51Testing软件测试网 W0I(c2Bw/n\rt
str.Open sql,conn,1,1'1,1表示只读;1,3表示插入数据;2,3表示修改数据
3o Y1z]J&c X]0str.MoveFirst'使游标指向第一个记录51Testing软件测试网b$F ~2SQ
sum=""51Testing软件测试网_6x{|K4w
WhileNotstr.EOF
@-bH:b-B+d*^0'msgbox str.Fields("username")51Testing软件测试网5A/X b[ zv5Kl7{
Fori=0tostr.Fields.Count-1'str.Fields.Count表示字段个数51Testing软件测试网zncC%f
sum=sum&str(i)&""'把整个记录显示出来51Testing软件测试网AVlLuv*o,?F
Next51Testing软件测试网U-Q MR%o _%z;T
Print sum&vbCRLF'打印所有查询的记录51Testing软件测试网1n _!B0@e/I z6T
str.MoveNext'使游标进入下一个51Testing软件测试网O7aJz'Yc3j
Wend
51Testing软件测试网6Mx&bZO"}Q3k"X({

51Testing软件测试网+_1h;~ j zM

str.Close'关闭数据集实例
Vf$He`M#dzHj0Setstr=Nothing51Testing软件测试网5qk;~g.}
Conn.Close'关闭数据库实例51Testing软件测试网l9\,a?MCp
SetConn=Nothing

posted on 2012-09-05 11:06  张飞_  阅读(567)  评论(0编辑  收藏  举报

导航