QTP Connecting MySQL database method

Source: Internet
Author: User
Tags dsn win32

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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.