Jump to content

EzMySql UDF - Use MySql Databases with autoit


Yoriz
 Share

Recommended Posts

  • 3 months later...
  • 1 month later...
  • 1 month later...

Sorry not used autoit or been on forum in a while, the download still works ok for me.

Edited by Yoriz
GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

  • 3 months later...

Hello,

is the connection to a distant web DB is possible

If Not _EzMySql_Open("myserver.net", "root", $Pass, "", "3306") Then
    MsgBox(0, "Error opening Database", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

If Not _EzMySql_Exec("CREATE DATABASE IF NOT EXISTS test_DB") Then
    MsgBox(0, "Error opening Database", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

If Not _EzMySql_SelectDB("_DB1") Then
    MsgBox(0, "Error setting Database to use", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

where can i sepcify the database name ? is 3306 the tcp port ?

how correctly do a connection to :

host : MyServer.net

user : root

$pass : 'pass'

DB : _DB1

Thanks for help

Regards

Edited by Reekod
Link to comment
Share on other sites

  • 2 months later...
  • 2 months later...

I'm trying to use _EzMySql_SelectDB() to verify that a database exists but it seems to always return 1 whether or not it's successful. A known bug?

Here's the EZMySQL UDF code:

; #FUNCTION# ====================================================================================================================

; Name...........: _EzMySql_SelectDB

; Description ...: Causes the database specified by db to become the default

; Syntax.........: _EzMySql_SelectDB($Database)

; Parameters ....: $Database - The new default database name

; Return values .: On Success - 1

; Return values .: On Failure - returns 0 and @error value

; 1 - A MySQL struct does not exist

; 2 - Dll Open Call failed

; Author ........: Yoriz

; Based on script: MySQL UDFs working with libmysql.dll by Prog@ndy

; ===============================================================================================================================

Func _EzMySql_SelectDB($Database)

If Not $hEzMySql_Ptr Then Return SetError(1, 0, 0)

Local $conn = DllCall($hEzMySql_Dll, "int", "mysql_select_db", "ptr", $hEzMySql_Ptr, "str", $Database)

If @error Then Return SetError(2, 0, 1)

Return 1

EndFunc ;==>_EzMySql_SelectDB

Link to comment
Share on other sites

  • 2 weeks later...

Have some problem with charset, may be somebody can help me.

In my programm on my Cirilic PC i Use code like this:

_EzMySql_Startup()
  $mysqlconnect =  _EzMySql_Open($host,$user,$password,$database,$port)
  If $mysqlconnect = 0 Then
   MsgBox(0,"Error", "Can`t connect to database")
   Return 0
  EndIf
  _EzMySql_Query("SET NAMES cp1251")

And all cyrilic data is ok. New Inserted and Selected from DB to GUI.

But on Chines PC, i have a problem with charset, i cant see in GUI Cirilic data and can`t insert in DB Cyrilic data.

What "SET NAMES " i should use on chines pc

HKEY_CURRENT_USER\Control Panel\International\Locale = 00000804.

My database has charset and collation utf8_general_ci, and mysql server also utf8_general_ci at any parameter.

Was tryed any chinese charset but result was in my GUI " ??????????? "

Edited by xtehbx
Link to comment
Share on other sites

  • 5 months later...

Capturing the SQL error code

I am testing out trying to insert a duplicate using _EzMySql_Exec .

What I'm finding is that even though the code fails because it is trying to insert a duplicate it doesn't come back with an error and an SQL error message. ( looking at @error and & _EzMySql_ErrMsg().

Is there any way of capturing SQL errors that occur?

Steven

Link to comment
Share on other sites

  • 3 months later...

I use this UDF and needed to escape string. That I little rewrote this func (based on Prog@ndy UDF function) to use with EzMySql.au3

Func _EzMySql_Real_Escape_String($From, $FromLen = Default)
If Not $hEzMySql_Ptr Then
  MsgBox(0, "", "errer")
  Return SetError(1, 0, 0)
EndIf
If $FromLen <= 0 Or $FromLen = Default Then $FromLen = StringLen($From)
Local $TO = DllStructCreate("char[" & $FromLen * 2 + 1 & "]")
Local $query = DllCall($hEzMySql_Dll, "int", "mysql_real_escape_string", "ptr", $hEzMySql_Ptr, "ptr", DllStructGetPtr($TO), "str", $From, "ulong", $FromLen)
If @error Then Return SetError(1, 0, 0)
Return StringLeft(DllStructGetData($TO, 1), $query[0])
EndFunc   ;==>_EzMySql_Real_Escape_String

ps: sorry for my English

Link to comment
Share on other sites

Hi guys,

I'm trying to set the connection to my DB,

I put my host, password, DB name and user to the _EzMySql_Open function.

That's all I did with it, now, when running the script I get all kinds of errors about "missing separator character after keyword".

Don't know exactly what it meens...

Anyone?

Link to comment
Share on other sites

  • 1 year later...

Nice job, It is work for Chinese, Great!

#include "EzMySql.au3"
#include <Array.au3>
#cs
http://www.autoitscript.com/forum/topic/116072-ezmysql-udf-use-mysql-databases-with-autoit/?hl=%2Bezmysql#entry1005253
#ce
If Not _EzMySql_Startup() Then
    MsgBox(0, "Error Starting MySql", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

Local $strHost = "127.0.0.1", $strUID = "root", $strPass = "**********", $strDB = "test"

If Not _EzMySql_Open($strHost, $strUID, $strPass, $strDB, "3306") Then
    MsgBox(0, "Error opening Database", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

If Not _EzMySql_Exec("CREATE DATABASE IF NOT EXISTS EzMySqlTest") Then
    MsgBox(0, "Error opening Database", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

If Not _EzMySql_SelectDB("EzMySqlTest") Then
    MsgBox(0, "Error setting Database to use", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

$sMySqlStatement = "CREATE TABLE IF NOT EXISTS TestTable (" & _
                   "RowID INT NOT NULL AUTO_INCREMENT," & _
                   "Name TEXT NOT NULL ," & _
                   "Age INT NOT NULL ," & _
                   "EyeColour TEXT NOT NULL ," & _
                   "HairColour TEXT NOT NULL ," & _
                   "PRIMARY KEY (`RowID`) ," & _
                   "UNIQUE INDEX RowID_UNIQUE (`RowID` ASC) );"

If Not _EzMySql_Exec($sMySqlStatement) Then
    MsgBox(0, "Error Creating Database Table", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

;Local $aEyeColours[7] = ["Amber","Blue","Brown","Grey","Green","Hazel","Red"]
;Local $aHairColours[6] = ["Brown","Black","Blond","Grey","Green","Pink"]
Local $aEyeColours[7] = ["琥珀","藍","棕","灰","綠","淡褐","紅"]
Local $aHairColours[6] = ["棕","黑","亞麻","灰","綠","桃紅"]

Local $sMySqlStatement = ""
For $i = 1 To 50 Step 1
    $sMySqlStatement &= "INSERT INTO TestTable (Name,Age,EyeColour,HairColour) VALUES (" & _
                        "'Person" & $i & "'," & _
                        "'" & Random(1, 100, 1) & "'," & _
                        "'" & $aEyeColours[Random(0, 6, 1)] & "'," & _
                        "'" & $aHairColours[Random(0, 5, 1)] & "');"
Next

If Not _EzMySql_Exec($sMySqlStatement) Then
    MsgBox(0, "Error inserting data to Table", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

;MsgBox(0, "Last AUTO_INCREMENT columnID2", _EzMySql_InsertID())
ConsoleWrite("!mysql> Last AUTO_INCREMENT columnID2  = " & _EzMySql_InsertID() & @CRLF)

;$aOk = _EzMySql_GetTable2d("SELECT Name,Age,EyeColour,HairColour FROM TestTable WHERE EyeColour = '"& $aEyeColours[Random(0, 6, 1)] & "';")
$aOk = _EzMySql_GetTable2d("SELECT Name,Age,EyeColour,HairColour FROM TestTable WHERE EyeColour = '"& $aEyeColours[Random(0, 6, 1)] & "'" & " AND Age >= '30' AND Age <= '40' " & ";")

$error = @error
If Not IsArray($aOk) Then MsgBox(0, $sMySqlStatement & " error", $error)
_ArrayDisplay($aOk, "2d Array Names of certain eyecolour")

MsgBox(0, "", "Following is how to get a row at a time of a query as a 1d array")
If Not _EzMYSql_Query("SELECT * FROM TestTable WHERE HairColour = '"& $aHairColours[Random(0, 5, 1)] & "' LIMIT 5;") Then
MsgBox(0, "Query Error", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

For $i = 1 To _EzMySql_Rows() Step 1
    $a1Row = _EzMySql_FetchData()
    _ArrayDisplay($a1Row, "Result: " & $i)
Next

_EzMySql_Exec("DROP TABLE TestTable")

_EzMySql_Close()
_EzMySql_ShutDown()
Exit
Link to comment
Share on other sites

I realize this is a very old topic, but whenever I go to parse through my MySQL DB any text because a number - not the text..

 ;****************************************
 ; Get all Non-Active Articles
 ;****************************************
 If Not _EzMYSql_Query('SELECT * FROM rars_articles WHERE IsActive = 1;') Then
 MsgBox(0, 'Query Error', 'Error: ' & @error & @CR & 'Error string: ' & _EzMySql_ErrMsg())
  Exit
 Else
  For $i = 1 To _EzMySql_Rows() Step 1
   $a1Row = _EzMySql_FetchData()
   MsgBox (0, "ID ", $a1Row[0] + 0)
   MsgBox (0, "Date", $a1Row[1] + 0)
   MsgBox (0, "Section", $a1Row[2] + 0)
   MsgBox (0, "Title", $a1Row[3] + 0)
   MsgBox (0, "Author", $a1Row[4] + 0)
   ;_ArrayDisplay($a1Row, "Result: " & $i)
  Next
 EndIf

Where this should return a Text return for "Section", "Title", and "Author" it is returning 0 for each of those rows. Yet _ArrayDisplay displays them correctly:

arraydisplay.png

What am I doing wrong, please?

Link to comment
Share on other sites

  • 2 weeks later...

I realize this is a very old topic, but whenever I go to parse through my MySQL DB any text because a number - not the text..

 ;****************************************
 ; Get all Non-Active Articles
 ;****************************************
 If Not _EzMYSql_Query('SELECT * FROM rars_articles WHERE IsActive = 1;') Then
 MsgBox(0, 'Query Error', 'Error: ' & @error & @CR & 'Error string: ' & _EzMySql_ErrMsg())
  Exit
 Else
  For $i = 1 To _EzMySql_Rows() Step 1
   $a1Row = _EzMySql_FetchData()
   MsgBox (0, "ID ", $a1Row[0] + 0)
   MsgBox (0, "Date", $a1Row[1] + 0)
   MsgBox (0, "Section", $a1Row[2] + 0)
   MsgBox (0, "Title", $a1Row[3] + 0)
   MsgBox (0, "Author", $a1Row[4] + 0)
   ;_ArrayDisplay($a1Row, "Result: " & $i)
  Next
 EndIf

Where this should return a Text return for "Section", "Title", and "Author" it is returning 0 for each of those rows. Yet _ArrayDisplay displays them correctly:

arraydisplay.png

What am I doing wrong, please?

Have you tried without the "+ 0"? If you want to use that to alter the index you should put it inside the braquets ie MsgBox (0, "ID ", $a1Row[1 + 0]) although I don't know why you would want to do something like that since you already know what index you need.

 

Link to comment
Share on other sites

  • 3 months later...

Maybe you wanna add this to EzMySql.au3

Func _EzMySql_Real_Escape_String($From, $FromLen = Default)
   If Not $hEzMySql_Ptr Then Return SetError(1, 0, 0)

   If $FromLen <= 0 Or $FromLen = Default Then $FromLen = StringLen($From)

   Local $TO = DllStructCreate("char[" & $FromLen * 2 + 1 & "]")
   Local $query = DllCall($hEzMySql_Dll, "int", "mysql_real_escape_string", "ptr", $hEzMySql_Ptr, "ptr", DllStructGetPtr($TO), "str", $From, "ulong", $FromLen)
   If @error Then Return SetError(2, 0, 0)
   Return StringLeft(DllStructGetData($TO, 1), $query[0])
EndFunc

This function allow me to escape some strings before I send them in a query to MySQL.

I did copy this from ">MySQL UDFs (without ODBC)" and adapted it to EzMySql, it works for me but maybe the autor of EzMySql should take a look of it.

Good Luck.

Edited by jofabian
Link to comment
Share on other sites

I don't use MySQL but it looks like for security reasons Addcslashes() would be a better shield. Second remark: your code is going to translate native AutoIt Unicode strings into ANSI, which is probably not suitable for many users.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • 2 months later...

In response to ChrisAnderson's Question. Try something like this. 

#include "EzMySql.au3"

$ipAddress = @IPAddress1

_EzMySql_Startup("\MYSQL")
$sqlConnect = _EzMySql_Open($ipAddress, "Username", "Password", "Database", 3306, "", 0) ;This opens a connection to the mysql database. Change Username Password and Database to YOUR mysql Database settings.

_EzMySql_Query('SELECT * FROM News') ;News is the table that I am getting data from. Change News to the name of the Table you want to query.
$myRows = _EzMySql_Rows() ;Just counts the rows that have data in them. 
_EzMySql_QueryFinalize()

$i = 1
$row = _EzMySql_GetTable2d('SELECT * FROM News') ;Makes a 2d array of data.
_EzMySql_QueryFinalize()

    Do
        MsgBox(0, "", $row[$i][1] & @CRLF & $row[$i][2])
    $i = $i + 1
     Until $i = $myRows + 1
_EzMySql_Close()

This works for me because I only have 2 fields of data. If you have a bunch of fields of data you would have to go much higher than two in the MsgBox. For example. 

Let's say I had 4 fields of Data. I would have to call the array like this:

MsgBox(0, "", $row[$i][1] & @CRLF & $row[$i][2] & @CRLF & $row[$i][3] & @CRLF & $row[$i][4])

This would display all 4 fields of information. This works because _EzMySql_GetTable2d() function makes a 2d array. You pull out the data from the array by using Column and Row. Column is number up and down. Row is number left and right. Which is why we call the variable $row like $row[1][1]. That says we are displaying data from Column1, Row1. Hopefully this helps anyone else struggling with this UDF as well. 

Edited by Morronic
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...