MQLMySQL Technical Reference MQL4
MQLMySQL Technical Reference MQL4
mqh
Metatrader 4 libmysql.dll
MySQL
MQL 4
Library
MQLMySQL.dll
database
MQLMySQL.mqh
The MQL4 program make calls to interface library, then interface library calls special functions
from standard libmysql.dll through the wrapper MQLMySQL.dll. The libmysql.dll dynamic link library
can be found in any MySQL related software or in MySQL distribution package. It is prepare connection
to the MySQL database and send queries to.
To make possible executing SELECT statements and fetching data from database, the
MQLMySQL.dll library was developed. It has number of functions to handle database cursors and
retrieve data using string type (char*/wchar_t* type). Maximal number of currently opened cursors is
set to 256. This value can be changed by recompiling MQLMySQL.DLL library. Highly recommended to
do not use so complicated SELECT statements. This can make data retrieving easy. If you need to use
complex SELECT statement, you may create database view based on your query and make selection
from view.
The functionality of MQLMySQL can be extended easily for other needs; in this case you may
study API functions of libmysql.dll (http://dev.mysql.com/doc/refman/5.0/en/c-api-functions.html) and
implement the functions you need.
Project consists of:
Filename Description
MQL4\Libraries\libmysql.dll MySQL standard library with C++ API.
MQL4\Libraries\MQLMySql.dll Developed library to extend libmysql.dll functionality for
MQL programs.
MQL4\Libraries\MQLMySql.def Definition file of MQLMySql.dll library, should be located in
the same directory with DLL.
MQL4\Include\MQLMySql.mqh Interface library which provides access to MySQL database for
MQL programs.
MQL4\Scripts\MySQL-XXX.mq4 Examples of using MQLMySQL.mqh interface library
MQLMySQL Technical Reference.docx This document you are reading.
Important: To enable interface between expert advisor and MySQL database you need to allow DLL
imports (Expert Advisors properties Common tab Allow DLL imports).
Important: The Metatrader 4 has been developed for x86 platforms (32-bit) and all dynamic libraries it
is used should be also built for x86 platforms. Please make sure that you are uses libmysql.dll was
compiled for 32-bit environment.
2
Interface variables
There are some interfaces variables can be used for error handling.
Interface functions
You may create connections (up to 32) to the MySQL database server by using interface
functions. The MySqlExecute function can be used to send SQL queries or special commands of MySQL
database (such as USE, SET and so on) and can be called after connection was created by MySqlConnect.
To close any created connection you may use MySqlDisconnect function.
3
Return Name Parameters Description
type
int pCursorID Cursor identifier, returned by function
MySqlCursorOpen
int pField Field number in SELECT clause (started
from 0)
int MySqlCursorOpen This function opens cursor for SELECT statement and returns
cursor identifier. You may open up to 256 concurrent cursors
(this restriction was made just in MQLMySql.dll, but this value
can be changed). In case when any error raised during SELECT
execution, this function returns -1.
int pConnection Database connection identifier
string pQuery SQL query (SELECT command)
void MySqlCursorClose This function used to close any opened cursor and free
memory.
Important: Do not forget to close cursor after using.
int pCursorID Cursor identifier, returned by function
MySqlCursorOpen
int MySqlCursorRows This function counts the number of rows was selected by
cursor. It can be used for fetching all rows in cycle.
int pCursorID Cursor identifier, returned by function
MySqlCursorOpen
bool MySqlCursorFetchRow This function should be used to fetch one row from cursors
record set into temporary buffer. After this operation it would
be possible to get values from rows fields.
int pCursorID Cursor identifier, returned by function
MySqlCursorOpen
int MySqlGetFieldAsInt You may use this function after row fetching to get field value,
represented as INTEGER. All fetched values are stored using
STRING data type, conversion of type based on MQL functions.
int pCursorID Cursor identifier, returned by function
MySqlCursorOpen
int pField Field number in SELECT clause (started
from 0)
double MySqlGetFieldAsDouble This function returns representation of fields value using
DOUBLE data type.
int pCursorID Cursor identifier, returned by function
MySqlCursorOpen
int pField Field number in SELECT clause (started
from 0)
datetime MySqlGetFieldAsDatetime This function returns representation of fields value using
DATETIME data type.
int pCursorID Cursor identifier, returned by function
MySqlCursorOpen
int pField Field number in SELECT clause (started
from 0)
string MySqlGetFieldAsString This function returns representation of fields value using
STRING data type. Synonym to MySqlGetRowField function
int pCursorID Cursor identifier, returned by function
MySqlCursorOpen
int pField Field number in SELECT clause (started
from 0)
4
Additions
1. Reading .ini files
Sometimes it is better to keep database credentials outside the MQL program. For this reason the
function ReadIni was integrated into MQLMySQL.dll:
The reading data from this .ini file into MQL variable can be done like:
string vServer = ReadIni("C:\\Metatrader4\\MQL4\\Experts\\MyConnection.ini", "MYSQL", "Server");
string Query = "INSERT INTO my_table(field1) VALUES (1); UPDATE my_table SET field1 = 2;";
To execute such query you can use MySqlExecute function. But you have to open the database
connection with pClientFlag = CLIENT_MULTI_STATEMENTS (decimal value 65536). For example:
5
Examples
Include MQLMySQL into your MQL project:
#include "..\Include\MqlMySql.mqh"
Connection to MySQL:
int DBConnection = MySqlConnect("localhost", "root", "ioctrl", "metatrader", 3306, "", 0);
if (DBConnection==-1)
{
Print("Error #", MySqlErrorNumber, ": ", MySqlErrorDescription);
return (1);
}
else Print ("Connected!");