Download MetaTrader 5

Retrieving single field from MySQL in MQL5 Solved!

To add comments, please log in or register
Carry on your private correspondence in your profile. It is secure!
Ingvar Engelbrecht
4501
Ingvar Engelbrecht 2011.12.27 23:45 

After a lot of trial and error, guesswork  and using the  debugger I got it solved!

Many days and many hours of frustration.  But,  "Nothing beats perserveance"

I guess it can be expanded to retrieve more than one field but for my purpose one field at the time works fine.

I have "faked" a "MySQL_Row"

struct Mysql_row

 {

   uchar header[8];        //Not used

   uchar data[50];          //maximum length on single field reply

};

 declare mysql_fetch_row like:

Mysql_row mysql_fetch_row(int result);

retrieve:

 //+------------------------------------------------------------------+
//| Get single field String value from MySQL                                      |
//+------------------------------------------------------------------+ 
 string getSTRField(string query2)
  {
   string ansiquery;
  
   Mysql_row my_row;
 
   ansiquery=UNICODE2ANSI(query2);  

   int length=StringLen(query2);
  
   int res2 = mysql_real_query(mysql,ansiquery,length);

   if(res2!=0)
    {
     Print("result of query  != 0!!  mysql:  "+IntegerToString(mysql) );
     Logger("getSTRField","query result", "mysgl:",IntegerToString(mysql) );
     int mysqlerr=mysql_errno(mysql);
     if (mysqlerr>0)
      {
       Print("Query: ",query2);
       Print("Returned error: ",ANSI2UNICODE(mysql_error(mysql)) );
       Logger( "Returned error: ", ANSI2UNICODE(mysql_error(mysql)) );   
      }
    }
   int res_store = mysql_store_result(mysql);

   if(res_store==0)
    {
     Print("ANSI2UNICODE(mysql_error(mysql)) ); = 0!!");
     Print("GetLastError MySQL:  " + DoubleToString(GetLastError(),0));
     Logger("getSTRField","GetLastError",DoubleToString(GetLastError(),0));
     return("");
    }
   int numOfRows = mysql_num_rows(res_store);
   int num_fields = mysql_num_fields(res_store);
  
   if(numOfRows==0)
    {
      Logger("No row found; ",query2);
      return("");
    }
    string head="";
    string dat ="";
   my_row = mysql_fetch_row(res_store);                //Only pick up first occurrence.
 
 //  for(int x = 0;x<8;x++)                            //Skip funny header
 //   {
 //     head = head + CharToString(my_row.header[x]);
 //   }
 
   for(int x = 0;x<50;x++)                             //get data, One field, maximum length 50
    {
      if(my_row.data[x] == 0) break;                   //end
      dat = dat + CharToString(my_row.data[x]);        //add it up in a string
    }
   mysql_free_result(res_store);
   return(dat);
   }
 

To add comments, please log in or register