dingmaotu / mql-sqlite3

SQLite3 binding for the MQL language (both 32bit MT4 and 64bit MT5)
MIT License
61 stars 36 forks source link

getColumn and getExtendedSql returning blank when string is over around 120 characters #4

Open beerb0x opened 3 years ago

beerb0x commented 3 years ago

I am using build 1280 of mt4 and I ran into this wierd problem, im not sure if its just me or it will effect others but i wanted to post my (dodgy) fix if anyone else runs into it. Any time i used getColumn on a table field that was larger than around 120characters or used getExtendedSql and the query was over 120 chars long i was getting a blank return value. Im no c programer but i dug around and worked out a dodgy fix just to get me out, I made a single line addition to StringFromUtf8Pointer in Common.mqh after the second call to MultiByteToWideChar.

string StringFromUtf8Pointer(intptr_t psz,int len)
  {
   if(len < 0) return NULL;
   string res;
   int required=MultiByteToWideChar(CP_UTF8,0,psz,len,res,0);
   StringInit(res,required);
   int resLength = MultiByteToWideChar(CP_UTF8,0,psz,len,res,required);
   res = StringFormat("%s",res);
   if(resLength != required)
     {
      return NULL;
     }
   else
     {
      return res;
     }
  }

Im no C programer and i have very little idea of what im doing so im unsure what caused this (maybe the copied string in memory doesnt quite jazz with mt4 perfectly) but my hack seems to have fixed it

beerb0x commented 3 years ago

OK the code formating isnt very good, basically i just added this line: res = StringFormat("%s",res); after the second call to MultiByteToWideChar and it fixed my issue

radulovicattila commented 3 years ago

Re: [dingmaotu/mql-sqlite3] getColumn and getExtendedSql returning blank when string is over around 120 characters (#4)

Hello beerb0x,

thank you for your comment and fix. I've no problem with the string lengths earlier. Is it possible that you had some special character in the field, and not the length was the problem? Radu

beerb0x commented 3 years ago

Im not sure what was causing it it seems to be a very strange problem to have. It was just a basic table with text & date fields populated via a Python script

beerb0x commented 3 years ago

Ok I finally added my sqllite code to my system thats using the ZeroMQ library and found the actual issue. It appears the SQLITE bindings are using earlier versions of the functions from the mql4-lib library. The bug appears to have been fixed with the newer mql4-lib functions. I think the fix would be to update the sqlite bindings with the current mql4-lib project.

string StringFromUtf8Pointer(intptr_t psz,int len)
  {
   if(len < 0) return NULL;
   string res;
   int required=MultiByteToWideChar(CP_UTF8,0,psz,len,res,0);
// need to include the NULL terminating character
   StringInit(res,required+1);
   int rlen=MultiByteToWideChar(CP_UTF8,0,psz,len,res,required);
   return rlen != required ? NULL : res;
  }

It appears the null terminating character is the problem and that is not included in the sqllite library

eabase commented 3 years ago

@beerb0x If you use tripple back-ticks (```) to enclose your code markup, we can actually read it.

beerb0x commented 3 years ago

No probs, new to github signed up to post this heh, ive fixed the others.