MetaTrader 5 MT5 Libraries EAX_Mysql – MySQL library – library MetaTrader 5

EAX_Mysql – MySQL library – library MetaTrader 5

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 😉

Examples:

Reading Data

#include <EAX\EAX_Mysql.mqh>

EAX_Mysql *db = new EAX_Mysql();

db.connect("myhost.mydomain.com", "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("myhost.mydomain.com", "myusername", "mypassword", "metatrader", "Ticks";
}

void OnTick() {
    MqlTick tick;
    SymbolInfoTick(_Symbol,tick);
    // Add a new dataset for table Ticks
    db.AddNew("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("bid", tick.bid);
    db.set("last", tick.last);
    db.set("time", TimeToString(tick.time,TIME_DATE) + " " + TimeToString(tick.time,TIME_SECONDS));
    db.set("volume", tick.volume);
    db.write();

}

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

Interacting with Data

EAX_Mysql *db = new EAX_Mysql();

db.connect("myhost.mydomain.com", "myusername", "mypassword", "metatrader", "mytable")

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

Installing:

Alternative:   Decorator - structural design pattern - library MetaTrader 5
  • 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”.

Issues:

  • 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.

Credits:

📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks