Hi Just Me - Just wanted to thank you for an awesome upgrade to panofish's incredible mysql api wrapper.
It bailed me out of a tight spot.
One thing that gave me trouble was actually using the data after I queried it.
(Ignorance on my part - obviously)
But I wrote a demo AHK script that uses your wrapper.
It's not much but it may help others traveling a similar path to mine.
To use it a single table database is needed.
There should be some data in the table too.
(I did not include a CREATE TABLE sql in this demo.)
I hope this will be helpful -- Thanks again !
Code below:
Code: Select all
;====================================================================
;
; Demo using mysql 2 library
;
; Programmer: NJ
; AutoHotkey: v1.1.04.00 (autohotkey_L ANSI version)
; Created to work with MySQLAPI
; Wrapper class for MySQL C API functions -> http://dev.mysql.com/doc/refman/5.5/en/c-api-functions.html
;
;====================================================================
#SingleInstance force
#NoENV ; Avoids checking empty variables to see if they are environment variables (recommended for all new scripts and increases performance).
SetBatchLines -1 ; have the script run at maximum speed and never sleep
ListLines Off ; a debugging option
outputdebug DBGVIEWCLEAR
#include mysql2.ahk ; pull from local directory
mysql=
dbName = test
server = localhost
user = root
pswrd = password
tablename = persons
;============================================================
; Build gui:
;============================================================
;Gui, 1:Default
Gui +resize
Gui, Add, Text, section w370, Database connect and Query Demo
Gui, Add, StatusBar
;-- buttons
Gui, Add, Button,x100 y+10 w100 h30 gdb_connect, Connect
Gui, Add, Button, y+10 w100 h30 gdb_ping, Ping
Gui, Add, Button, y+10 w100 h30 gdb_select, Select
Gui, Add, Button, y+10 w100 h30 gdb_store, Use Data
Gui, Add, Button,x100 y+20 w100 h30 gdb_disconnect, Disconnect
Gui, Add, Text, section y+30 w370 vDBSTAT, Database: Not Connected
Gui, Show, w600 h400,MySQL 2 debugger
return
;============================================================
;
;============================================================
;------------------------------------------------------------
db_connect:
try {
mysql := new MySQLAPI ; instantiates an object using this class
mysql.Real_Connect(server, user, pswrd, dbName)
}
catch e
{
MsgBox,16, Error, % "Failed to create connection. Check your Connection string and DB Settings!`n`n"e
ExitApp
}
gosub db_ping
if (Result = 0)
{
;MsgBox,,CONFIRM:, Database connected..
GuiControl,,DBSTAT, Database Connected..
}
else
{
MsgBox,,CONNECT FAILURE:, Failed to connect !
}
return
;------------------------------------------------------------
db_disconnect:
mysql=
GuiControl,,DBSTAT, Database Disconnected
return
;------------------------------------------------------------
db_select:
result = -1
sql = SELECT * FROM %dbName%.%tablename% ;-- generic test script to get all data from the person table in the test database
result := mysql.Query(sql)
if result = 0
{
;MsgBox,,CONFIRM:, Query executed `nresult = %result%
GuiControl,,DBSTAT, Database: Query executed
}
else
{
MsgBox,,ERROR:, Query failed ! `nresult = %result%
}
return
;------------------------------------------------------------
db_ping:
result := -1
result := mysql.Ping()
if result = 0
{
;MsgBox,,CONFIRM:, Ping confirms connection `nresult = %result%
GuiControl,,DBSTAT, Database: Ping confirms connection
}
else
{
MsgBox,,ERROR:, Ping failed ! ;`nresult = %result%
}
return
;------------------------------------------------------------
db_store:
resultPtr := mysql.Store_Result()
if resultPtr > 0
{
fieldCount := mysql.Field_Count() ;-- returns the number of columns in the data set
rowCount := mysql.Num_Rows() ;-- returns the number of row in the data set
Loop
{
;thisRow := mysql.GetNextRow(resultPtr)
row := mysql.Fetch_Row(resultPtr) ;-- loop through rows until Fetch_Row returns NULL
If (row = 0 || row == "")
Break
; Get a pointer on a table of lengths (unsigned long)
lengths := mysql.Fetch_Lengths(resultPtr) ;-- column lengths for the given row
Loop %fieldCount% ;-- loop through the columns (fields) of this row
{
J := A_Index - 1
If (Len := NumGet(Lengths + 0, 4 * J, "UInt"))
field := (StrGet(NumGet(row + 0, A_PtrSize * J, "UPtr"), Len, "UTF-8"))
else
field=
;MsgBox,,CONFIRM:, field = %field% `n Len = %Len% `n J = %J%
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$", "")
MsgBox,,CONFIRM:, resultString = %resultString%
}
else
{
MsgBox,,ERROR:, Missing Data ! `nresult = %result%
}
return
;============================================================
; when you click x or close button
;============================================================
GuiClose:
ExitApp