Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2012.08.22 08:36

EAX_Mysql - MySQL library - library for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

Published by:
Michael Schoen
votes: 28
eax_mysql.mqh (29.79 KB)view

Accidently I ran into MQL5 and was forced to pull a MySQL library together. As for any library I hope the examples show how the library can be used. As for any library actually the most important things are examples how to use it ;)


Reading Data

#include <EAX\EAX_Mysql.mqh>

EAX_Mysql *db = new EAX_Mysql();

db.connect("", "myusername", "mypassword", "mydatabase", "mytable");

int iResults = db.read_rows("SELECT password, COUNT(*) as Hits FROM users GROUP BY password");
for (int i=0; i < iResults; i++) {
   string password = (string) db.get("password",i);
   int    hits     = (int) db.get("Hits", i);

Feeding Data

#include <EAX\EAX_Mysql.mqh>

// global
EAX_Mysql *db = new EAX_Mysql();

void OnInit() {
    db.connect("", "myusername", "mypassword", "metatrader", "Ticks";

void OnTick() {
    MqlTick tick;
    // Add a new dataset for table Ticks
    // fill it with values..
    db.set("symbol", _Symbol);
    // You can send digits to digit DB fields if MySQL can convert
    db.set("ask", tick.ask);
    db.set("last", tick.last);
    db.set("time", TimeToString(tick.time,TIME_DATE) + " " + TimeToString(tick.time,TIME_SECONDS));
    db.set("volume", tick.volume);


void OnDeinit() {
   // clean up memory
   delete db;

Interacting with Data

EAX_Mysql *db = new EAX_Mysql();

db.connect("", "myusername", "mypassword", "metatrader", "mytable")

// Select Table AgentsOnline (required to identify the correct Primary Key)"AgentsOnline");
// Read Dataset with Primary Key 5"5");
// modify any column,
db.set("lastupdate", (string) TimeLocal());
// write it back


  • Identify your MQL5 Data Directory (MQL5).
  • Download Connector/C (libmysql) for your MetaTrader environment (32 or 64bit) and put libymsql.dll into "MQL5\Libraries".
  • create an folder EAX under Include.
  • Put EAX_Mysql.mqh in "MQL5\Include\EAX".


  • As it uses internally a shared connection pointer the library can't handle more than one DB connection at a time (I had no need to add/migrate a connection pooling).
  • no real error (mysql) handling, dropping db connection.
  • no error handling/reconnection to the DB (=> db pooling).
  • no support for multiple column primary keys.
  • still beta - please drop me an email if you want to beta/test.
  • DataTypes are not handled (internally just strings) must be on code/database.
  • Database communication is ISO - too lazy to adjust the pointer/string arithmetic for UTF-8.


ExtObjects ExtObjects

Dedicated functions to read and write object properties.

Momentum Color Fill Momentum Color Fill

The Momentum Technical Indicator measures the change of price of a financial instrument over a given time span.

DaysOfWeekCheck DaysOfWeekCheck

The function is used to determine weekends on a server. It will be especially useful to those who use OnTimer() function in their Expert Advisors for events handling.

OpenBuyPosition OpenBuyPosition

The script is developed for buying with fixed Stop Loss and Take Profit values in points from the current price.