How to connect qtp to MySQL database

Source: Internet
Author: User

1. First install the MySQL DATA driver. qtp is connected to MySQL in windows, 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 the MySQL driver by default.

3. Add a default data source

Control Panel-> Administrative Tools-> data source ODBC-> System DNS-> Add-> in the "Create data source" dialog box, select

"MySQL ODBC 5.1 Driver"-> complete-> enter the connection dialog box-> fill in and click "test" to check whether the configuration is successful.

4. Use the qtp script to connect to the MySQL database

If step 3rd is performed and the default Mysql Data Source is installed on the Tianji, the script can be written as follows:

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}; database = extmail; Pwd = 123456; Port = 3306; server = 192.168.2.52; uid = root"
Conn. Open connectionstring
If conn. State <> 0 then
Reporter. reportevent micpass, "testing", "database connected"
Else
Reporter. reportevent micfail, "testing", "database connection failed"
End if

If no 3rd steps are performed and no default data source is added, you can connect to the data source in normal mode.

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}; database = extmail; Pwd = 123456; Port = 3306; server = 192.168.2.52; uid = root"
Conn. Open connectionstring
If conn. State <> 0 then
Reporter. reportevent micpass, "testing", "database connected"
Else
Reporter. reportevent micfail, "testing", "database connection failed"
End if

5. qtp script for database query

Dim Conn, STR, SQL, I, Sum
Set conn = Createobject ("ADODB. Connection") 'create a database 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}; database = extmail; Pwd = 123456; Port = 3306; server = 192.168.2.52; uid = root"
Conn. Open connectionstring
If conn. State <> 0 then
Reporter. reportevent micpass, "testing", "database connected"
Else
Reporter. reportevent micfail, "testing", "database connection failed"
End if

Set STR = Createobject ("ADODB. recordset") 'create a dataset instance

'Query the database
SQL = "select * From Manager where type = 'postmaster '"
Str. Open SQL, Conn, '1, 1 indicates read-only; 1, 3 indicates data insertion; 2, 3 indicates data modification
Str. movefirst 'points the 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) & "" 'shows the entire record.
Next
Print sum & vbcrlf print all query records
Str. movenext 'causes the cursor to enter the next
Wend

Str. Close 'Close the dataset instance
Set STR = nothing
Conn. Close 'Close the database 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.