Chinaunix首页 | 论坛 | 博客
  • 博客访问: 591576
  • 博文数量: 60
  • 博客积分: 3993
  • 博客等级: 中校
  • 技术积分: 1572
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 17:08
文章分类

全部博文(60)

文章存档

2012年(7)

2011年(35)

2010年(8)

2009年(7)

2008年(3)

分类: Python/Ruby

2011-11-15 14:53:19

 mysql.au3.txt    mysql-connector-odbc-3.51.29-win32.part1.rar    mysql-connector-odbc-3.51.29-win32.part2.rar   

Title: MySQL UDF functions
Filename: MySQL.au3
Description: A collection of functions for interacting with MySQL
Author: cdkid
Version: 1.6
Requirements: Autoit V3 with COM support.
Note: Make sure to add _MySQLEnd to the end of any script you use this with or you will have open connections to your DB left open!!!!
Note2: at the end of your script i find that it helps to turn your connection object (in the examples i use $sql) to "" so add $sql = "" to the end
Note3: I think it is because using COM from a non-server connecting to a DB on a server, but these can take an EXTREMELY long time.
---> functions:
_MySQLConnect()
Open a connection to the specified Database
_MySQLEnd()
Close the connection (read notes!)
_AddRecord()
Adds a record to a specified table
_DeleteRecord()
Deletes a record from the specifed table.
_Query()
Execute a query to the database
_CreateTable()
Adds a table to the database
_CreateColumn()
Adds a column to the given table
_DropCol()
Deletes a column from the given table
_DropTbl()
Deletes a table from the given DB
_CountRecords()
Counts the number of records in the given column
_CountTables()
Counts the number of tables in the database
_GetColNames()
Gets the names of all the columns in the given table
_GetTblNames()
Gets the names of all the tables in the database
_GetColvals()
Gets all of the values of the specified column
_GetColType()
Gets the DATA TYPE of the specified column
_GetColCount()
Gets a count of all columns in the specified table
_MySQLTableExists()
Find out whether or not a specified table exists
_GetDBNames()
Get a list & count of databases on the current server.
_ChangeCon()
Change your connection string.

 

here are a few examples
#1 start a connection, do a SELECT statement, write the return value to 'C:\test.txt' then end connection

 

  1. $sql = _MySQLConnect("sa","sa","mydb","mywebsite.com")
  2. $var = _Query($sql,"SELECT * FROM mytable WHERE user = 'username'")
  3. With $var
  4. While NOT .EOF
  5. FileWriteLine("c:\test.txt",.Fields("user_name").value & @CRLF)
  6. .MoveNext
  7. WEnd
  8. EndWith
  9. _MySQLEnd($sql)

 

#2 start a connection, add a record, delete a record, end connection

 

  1. dim $values[5]
  2. $values[0] = "col1"
  3. $values[1] = "col2"
  4. $values[2] = "col3"
  5. $values[3] = "col4"
  6. $values[4] = "";make sure u have one extra blank element

  7. $sql = _MySQLConnect("sa","sa","mydb","mywebsite.com")
  8. _AddRecord($sql, 'mytable', $values, $values)
  9. ;this executes a query ("INSERT INTO mytable (col1, col2, col3, col4) VALUE (col1, col2, col3, col4)
  10. ;which adds a record where col1, col2, col3, and col4 have the value "col1", "col2" etc
  11. _DeleteRecord($sql, "mytable", "username", "user")
  12. ;that executes a MySQL query ("DELETE FROM mytable WHERE username = 'user';)
  13. _MySQLEnd($sql)

 

#3 - Connect, create a table, add a record, count records in the table, drop the table, end connection

 

 

  1. $sql = _MySQLConnect('sa','sa','mydb','mywebsite.com')
  2. _CreateTable($sql, 'testtable', 'tt_id')
  3. _AddRecord($sql, 'testtable', 'tt_id', 1)
  4. $count = _CountRecords($sql, 'testtable', 'tt_id', 1)
  5. ;this executes the query

 

#4 - Connect, get a list of databases, MessageBox the result

 

  1. $sql = _MySQLConnect('sa','sa','','mywebsite.com')
  2. $dbs = _GetDbNames($sql)
  3. For $i in $dbs
  4. MsgBox(0,'',$i)
  5. Next
  6. _MySQLEnd($sql)

 

if you're getting errors...i've found a good way to track em. this was in the idea lab, written by SvenP
put this at the top:

 

  1. $objErr = ObjEvent("AutoIt.Error","MyErrFunc")

and then this anywhere

 

  1. Func MyErrFunc()

  2. $hexnum=hex($objErr.number,8)

  3. Msgbox(0,"","We intercepted a COM Error!!" & @CRLF & @CRLF & _
  4.              "err.description is: " & $objErr.description & @CRLF & _
  5.              "err.windescription is: " & $objErr.windescription & @CRLF & _
  6.              "err.lastdllerror is: " & $objErr.lastdllerror & @CRLF & _
  7.              "err.scriptline is: " & $objErr.scriptline & @CRLF & _
  8.              "err.number is: " & $hexnum & @CRLF & _
  9.              "err.source is: " & $objErr.source & @CRLF & _
  10.              "err.helpfile is: " & $objErr.helpfile & @CRLF & _
  11.              "err.helpcontext is: " & $objErr.helpcontext _
  12.             )
  13. exit
  14. EndFunc

 

example

 

  1. #include <MySQL.au3>
  2. #include <Array.au3>

  3. ;listDB()
  4. ;listTB()
  5. listCOL()

  6. Func listDB()
  7.     $sql = _MySQLConnect("root","autoit","","localhost")
  8.     $dbs = _GetDbNames($sql)
  9.     _arrayDisplay($dbs)
  10.     _MySQLEnd($sql)
  11. EndFunc

  12. Func listTB()
  13.     $sql = _MySQLConnect("root","autoit","autoit","localhost")
  14.     $tbs = _GetTblNames($sql)
  15.     _arrayDisplay($tbs)    
  16.     _MySQLEnd($sql)
  17. EndFunc    

  18. Func listCOL()
  19.     $sql = _MySQLConnect("root","autoit","autoit","localhost")
  20.     $var = _Query($sql,"SELECT * FROM test")
  21.     With $var
  22.         While NOT .EOF
  23.             MsgBox(0,"listCOL","id=" & .Fields("id").value & " name=" & .Fields("name").value & @CRLF)
  24.             .MoveNext
  25.         WEnd
  26.     EndWith
  27.     _MySQLEnd($sql)
  28. EndFunc
阅读(3296) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~