AutoHotkey Community

It is currently May 27th, 2012, 12:04 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: January 13th, 2011, 6:00 pm 
Offline

Joined: February 6th, 2007, 12:30 am
Posts: 142
Location: Michigan
If you work with mysql like me, then you'll appreciate the simplicity of this include which allows you to easily connect and query to your mysql database.

Unfortunately, it is not compatible with autohotkey_L unicode.
It currently only works with autohotkey basic and autohotkey_L (ansi).

Can anyone help change this code so that it will be compatible with autohotkey_L (unicode)?
I am sure many people can use it and it will help encourage the adoption of autohotkey_L (unicode).

Here is an example of how simple it is to connect to a mysql database and execute a query using this include.

Code:
#Include mysql.ahk

; Connect to the sql database at the top of the program
db := dbConnect("hostname","userid","password","databasename")     

sql =
(
   SELECT request,
          user,
          priority
     FROM table1
     WHERE Status = 'Pending'
     ORDER BY requestdate DESC
)

result := dbQuery(db, sql)


This is the mysql.ahk include file:

Code:
;============================================================
; mysql.ahk
;
;   Provides a set of functions to connect and query a mysql database
;============================================================

;============================================================
; The fileinstall command does 2 things.
; 1. When this ahk program is compiled into an exe, fileinstall indicates which files should be embedded inside the exe.
; 2. When the exe version of the program is run, fileinstall extracts the embedded file to the specified folder.
;
; note: #include files are automatically embedded at compile time, so you don't need to use fileinstall for them.
;============================================================

FileInstall, libmysql.dll, %A_AppData%\libmysql.dll, 1

;============================================================
; Connect to mysql database and return db handle
;
; host = DTWRO-WS0061   
; user = alan
; password = reddog
; database = rush
;============================================================

dbConnect(host,user,password,database){   

   if (A_IsCompiled) {
      ExternDir := A_AppData
   } else {
      ExternDir := A_WorkingDir
   }

   hModule := DllCall("LoadLibrary", "Str", ExternDir "\libmySQL.dll")
      
   If (hModule = 0)
   {
      MsgBox 16, MySQL Error 233, Can't load libmySQL.dll from directory %ExternDir%
      ExitApp
   }

   db := DllCall("libmySQL.dll\mysql_init", "UInt", 0)
         
   If (db = 0)
   {
      MsgBox 16, MySQL Error 445, Not enough memory to connect to MySQL
      ExitApp
   }

   connection := DllCall("libmySQL.dll\mysql_real_connect"
         , "UInt", db
         , "Str", host       ; host name
         , "Str", user       ; user name
         , "Str", password   ; password
         , "Str", database   ; database name
         , "UInt", 3306   ; port
         , "UInt", 0   ; unix_socket
         , "UInt", 0)   ; client_flag

   If (connection = 0)
   {
      HandleMySQLError(db, "Cannot connect to database")
      Return
   }

   serverVersion := DllCall("libmySQL.dll\mysql_get_server_info", "UInt", db, "Str")
         
   ;MsgBox % "Ping database: " . DllCall("libmySQL.dll\mysql_ping", "UInt", db) . "`nServer version: " . serverVersion

   return db

}

;============================================================
; mysql error handling
;============================================================

HandleMySQLError(db, message, query="") {        ; the equal sign means optional
   errorCode := DllCall("libmySQL.dll\mysql_errno", "UInt", db)
   errorStr := DllCall("libmySQL.dll\mysql_error", "UInt", db, "Str")
   MsgBox 16, MySQL Error: %message%, Error %errorCode%: %errorStr%`n`n%query%
   Return
}

;============================================================
; mysql get address
;============================================================

GetUIntAtAddress(_addr, _offset)
{
   local addr

   addr := _addr + _offset * 4

   Return *addr + (*(addr + 1) << 8) +  (*(addr + 2) << 16) + (*(addr + 3) << 24)
}

;============================================================
; process query
;============================================================

dbQuery(_db, _query)
{
   local resultString, result, requestResult, fieldCount
   local row, lengths, length, fieldPointer, field

   query4error := RegExReplace(_query , "\t", "   ")    ; convert tabs to spaces so error message formatting is legible
   result := DllCall("libmySQL.dll\mysql_query", "UInt", _db , "Str", _query)
         
   If (result != 0) {
      errorMsg = %_query%
      HandleMySQLError(_db, "dbQuery Fail", query4error)
      Return
   }
   
   requestResult := DllCall("libmySQL.dll\mysql_store_result", "UInt", _db)
   
   if (requestResult = 0) {    ; call must have been an insert or delete ... a select would return results to pass back
      return
   }
   
   fieldCount := DllCall("libmySQL.dll\mysql_num_fields", "UInt", requestResult)

   Loop
   {
      row := DllCall("libmySQL.dll\mysql_fetch_row", "UInt", requestResult)
      If (row = 0 || row == "")
         Break

      ; Get a pointer on a table of lengths (unsigned long)
      lengths := DllCall("libmySQL.dll\mysql_fetch_lengths" , "UInt", requestResult)
            
      Loop %fieldCount%
      {
         length := GetUIntAtAddress(lengths, A_Index - 1)
         fieldPointer := GetUIntAtAddress(row, A_Index - 1)
         VarSetCapacity(field, length)
         DllCall("lstrcpy", "Str", field, "UInt", fieldPointer)
         resultString := resultString . field
         If (A_Index < fieldCount)
            resultString := resultString . "|"     ; seperator for fields
      }
     
      resultString := resultString . "`n"          ; seperator for records 

   }

   ; remove last newline from resultString
   resultString := RegExReplace(resultString , "`n$", "")    
   
   Return resultString
}

_________________
http://www.panofish.net


Last edited by panofish on January 18th, 2011, 7:44 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 13th, 2011, 6:45 pm 
Offline

Joined: October 15th, 2007, 3:10 pm
Posts: 790
Location: England
Read the bit about DllCall compatibility issues between ANSI and Unicode versions here.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 13th, 2011, 7:22 pm 
Offline

Joined: February 6th, 2007, 12:30 am
Posts: 142
Location: Michigan
OceanMachine wrote:
Read the bit about DllCall compatibility issues between ANSI and Unicode versions here.


Thanks... I did, but it's a bit over my head at this time. I'm not sure if I understand enough to convert the code as needed.

_________________
http://www.panofish.net


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2011, 9:13 am 
Replacing "Str" with "AStr" in your DllCalls will get you running with autohotkey_L (unicode).

Nice script btw. :wink:


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2011, 8:27 pm 
Offline

Joined: July 9th, 2009, 1:13 am
Posts: 140
Anonymous wrote:
Replacing "Str" with "AStr" in your DllCalls will get you running with autohotkey_L (unicode).

Nice script btw. :wink:


I tried this, and it causes the libary to fail to load. Using "Str" allows it to load, but then it crashes.

Has anyone been able to get this working in AHK_L (unicode)?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2011, 10:57 pm 
Offline

Joined: July 9th, 2009, 1:13 am
Posts: 140
Nvm, I got a working sql connection with AHK_L's Com scriptControl


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2011, 6:02 am 
Offline

Joined: August 8th, 2009, 2:25 am
Posts: 14
hi!
thanks for the mysql.ahk :)
i'm trying to use it, and get issue:
right after i use
Code:
dbConnect(host,user,password,database)
with all correct values, it says:

MySQL Error: Cannot connect to database
Error 1045: Access denied for user 'user'@'host' (using password: YES)

The thing is it shows correct 'user' and 'host', but each in single quotes.
could it be the reason it fails? the quotes?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2011, 12:26 am 
Offline

Joined: August 8th, 2009, 2:25 am
Posts: 14
Hi guys!
Any updates on library’s compatibility with autohotkey_L?
I’m trying to use it, but unfortunately it crashes (dbConnect works, but dbQuery crashes the autohotkey_L).
I tried to put some breakpoints, and found that (within dbQuery) first call (libmySQL.dll\mysql_query) doesn’t break anything, but then it crashes at HandleMySQLError(_db, "dbQuery Fail", query4error). I thought maybe it could help you to debug the issue…
Anyway, thanks for nice library, I was using it with classic autohotkey a lot!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 29th, 2011, 9:16 pm 
Offline

Joined: February 6th, 2007, 12:30 am
Posts: 142
Location: Michigan
The latest mysql libary is available here:

http://www.autohotkey.com/forum/topic77860.html

Unfortunately, it stills needs to be converted to Unicode. :(

_________________
http://www.panofish.net


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, Google Feedfetcher, Klark92, Yahoo [Bot] and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group