MySQL for new MQL4 (tested in build 600) – library MetaTrader 4

Author:

Originally created by Russell, then modified by vedroid and now forked by lukins

Since build 600 of MT4 was released, it’s no longer easy to communicate with DLLs that use ANSI format for strings because strings in MQL4 are now in UNICODE format (read more here).

This library is based on vedroid’s modification of Russel’s MySQL wrapper and uses techniques taken from EAX_Mysql library and from snippets posted by gchrmt4

Any contribution appreciated.

Example:

#include <mql4-mysql.mqh>

string  host     = "localhost";
string  user     = "root";
string  pass     = "123456";
string  dbName   = "information_schema";

int     port     = 3306;
int     socket   = 0;
int     client   = 0;

int     dbConnectId = 0;
bool    goodConnect = false;

int start () {
    goodConnect = init_MySQL(dbConnectId, host, user, pass, dbName, port, socket, client);
    
    if ( !goodConnect ) {
        return (1); // bad connect
    }

    //+-------------------------------------------------------------------
    //| Fetch multiple columns in multiple rows
    //+-------------------------------------------------------------------    
    string query = StringConcatenate(
        "SELECT `TABLE_NAME`, `TABLE_ROWS`, `CREATE_TIME`, `CHECK_TIME` ",
            "FROM `TABLES` ",
            "WHERE `TABLE_SCHEMA` = \'mysql\'");
    string data[][4];   // important: second dimension size must be equal to the number of columns
    int result = MySQL_FetchArray(dbConnectId, query, data);
    
    if ( result == 0 ) {
        // Print("0 rows selected");
    } else if ( result == -1 ) {
        // Print("some error occured");
    } else {
        // Print("Query was successful. Printing rows...");
        int num_rows = ArrayRange(data, 0);
        int num_fields = ArrayRange(data, 1);
        
        for ( int i = 0; i < num_rows; i++) {
            string line = "";
            
            for ( int j = 0; j < num_fields; j++ ) {
               string value = data[i][j];
                line = StringConcatenate(line, value, ";");
            }
            Print(line);
        }
    }
    
    //+-------------------------------------------------------------------
    //| Single row fetch
    //| Similar to array fetch
    //+-------------------------------------------------------------------
    string row[][4];   // important: second dimension size must be equal to the number of columns
    string rowQuery = StringConcatenate("SELECT `TABLE_NAME`, `TABLE_ROWS`, `CREATE_TIME`, `CHECK_TIME` ",
            "FROM `TABLES` ",
            "WHERE `TABLE_SCHEMA` = \'mysql\' ",
            "LIMIT 0,1");
    int row_result = MySQL_FetchArray(dbConnectId, rowQuery, row);
    line = "One row query: ";
    for(int r=0; r < ArrayRange(row, 1); r++) {
      line = StringConcatenate(line, row[0][r], ";");
    }
    Print(line);
    
    //+-------------------------------------------------------------------
    //| Sample INSERT
    //| It's just an example, nobody can insert into information_schema.
    //+-------------------------------------------------------------------
    /*string insertQuery  = StringConcatenate(
        "INSERT INTO `CHARACTER_SETS` VALUES (", "sometext"
                                        , "," , "oneMoreText"
                                        , "," , "andVarialbe"
                                        , "," , "nextWillBeNumber"
                                        , "," , 2
                                        , ")"
        );
    if ( MySQL_Query(dbConnectId, insertQuery) ) {
        Print("insert good");
    }*/
    
    
    //+-------------------------------------------------------------------
    deinit_MySQL(dbConnectId);
    return (0);
}
//+------------------------------------------------------------------+


Source:

  News events and market times on your chart - indicator MetaTrader 4

Source is distributed at Github repository. Any contribution is highly appreciated.


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