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