1. First install MySQL data-driven, QTP operation under Windows System connection MySQL, so download mysql-connector-odbc-5.1.8-win32.msi
: Http://mysql.mirrors.pair.com/Downloads/Connector-ODBC/5.1/mysqlconnector-odbc-5.1.8-win32.msi
2. Install MySQL driver and install it by default.
3. Add a default data source
Control Panel, management tools, data source odbc-> system dns-> Add, in the Create Data Source dialog box, select
"Mysql ODBC 5.1 Driver", completing the Enter Connection dialog box, click "Test" to see if it is successful and the configuration is complete.
4.QTP scripting to connect to MySQL database
If you do the 3rd step, the MySQL default data source, the script can be written as:
Dim Conn
Set conn=createobject ("ADODB. Connection ")
Const connectionstring= "Dsn=mysql_mail;database=extmail; pwd=123456; port=3306; server=192.168.2.52; Uid=root "
' Const connectionstring= ' driver={mysql ODBC 5.1 Driver};D atabase=extmail; pwd=123456; port=3306; server=192.168.2.52; Uid=root "
Conn.Open ConnectionString
If Conn.state<>0 Then
Reporter.reportevent Micpass, "testing", "Connection to database Success"
Else
Reporter.reportevent Micfail, "testing", "Connection database Failed"
End If
If you do not follow the 3rd step and do not add a default data source, you can connect in the normal way
Dim Conn
Set conn=createobject ("ADODB. Connection ")
' Const connectionstring= ' dsn=mysql_mail;database=extmail; pwd=123456; port=3306; server=192.168.2.52; Uid=root "
Const connectionstring= "Driver={mysql ODBC 5.1 Driver};D atabase=extmail; pwd=123456; port=3306; server=192.168.2.52; Uid=root "
Conn.Open ConnectionString
If Conn.state<>0 Then
Reporter.reportevent Micpass, "testing", "Connection to database Success"
Else
Reporter.reportevent Micfail, "testing", "Connection database Failed"
End If
5.QTP Script Implementation Query database
Dim Conn,str,sql,i,sum
Set conn=createobject ("ADODB. Connection ") ' Create DB instance
Const connectionstring= "Dsn=mysql_mail;database=extmail; pwd=123456; port=3306; server=192.168.2.52; Uid=root "
' Const connectionstring= ' driver={mysql ODBC 5.1 Driver};D atabase=extmail; pwd=123456; port=3306; server=192.168.2.52; Uid=root "
Conn.Open ConnectionString
If Conn.state<>0 Then
Reporter.reportevent Micpass, "testing", "Connection to database Success"
Else
Reporter.reportevent Micfail, "testing", "Connection database Failed"
End If
Set str=createobject ("ADODB. Recordset ") ' to create a dataset instance
' Query database
Sql= "SELECT * from manager where type= ' postmaster '"
Str. Open sql,conn,1,1 ' is read-only; 1,3 represents inserting data; 2,3 means modifying data
Str. MoveFirst ' to point a cursor to the first record
Sum= ""
While not str. Eof
' MsgBox str. Fields ("username")
for i=0 to Str. Fields.count-1 ' Str. Fields.Count indicates the number of fields
Sum=sum & Str (i) & "" To show the entire record
Next
Print sum & VbCRLF ' prints records for all queries
Str. MoveNext ' causes the cursor to enter the next
Wend
Str. Close ' Closing the dataset instance
Set str=nothing
Conn.close ' shutting down the DB instance
Set conn=nothing