I have binary std::string and I need insert it into the BLOB (MySQL) using simple data layer I have. So, I need to execute query: ExecuteSQL((LPTSTR)strQ).
When I am creating this query string (strQ) I cannot add anything to the string after I add this binary string - it just kind if terminated and nothing can be added. I do not want to use mysql_real_escape_string because i want to keep it not only for MySQL.
Anybody to HELP PLEASE!!!
Assuming you have code that looks something like this:
std::string s = ... // populate string somehow
ExecuteSQL( (LPCSTR) s );
Then you have several problems. Forstly, the cast won't work. In C++, whenever you use a cast you are almost certainly doing something incorrect which will break your code. You need to create a null-terminated string using the std::string member function c_str():
ExecuteSQL( s.c_str() );
However, this may not fix all your your problems because you say you hava a binary string. If that string contains the zero byte, then your SQL will terminate at that character rather than the end of string. In that case you probably need to investigate binding your values explicitly.
Edit: For details of how to bind a parameter to a MySQL statement, see http://dev.mysql.com/doc/refman/5.1/en/mysql-stmt-bind-param.html
Related
I have a strange question about the using of libdb2.so and libdb2o.so. My C++ Program have an string with length/size 33. I'm trying to write that string inside a database column of character which have a size of 35 character. Important: That string have a german umlauts (ü) and several special characters. It looks like that:
Xüxxxxx / Xxxxxxxxxxxx-Xxxxxxxxxxx
If my program ran with the hard-linked libdb2.so it ran perfectly and I have no SQL Error for many month. After a view month I thought would be a great idea to use the libdb2o.so which I did not hard-linked inside my C++ Program. All other SQL Statements went well, but my INSERT-SQLs got an error like that:
[IBM][CLI Driver][DB2/AIX64] SQL0302N The value of a host variable in the EXECUTE or OPEN statement is out of range for its corresponding use. SQLSTATE=22001
After some analysis I regonized that I could have an encoding problem, but not inside my std::string. The size of std::string did not changed. It is still 33 long. If I replaced my umlauts to normal character (ü => u) it worked fine, but it is not what I want.
I thought if I using libdb2o.so I have the standard encoding UTF8, but it looks like it could not be, or? If I tried to set the UTF8 inside my connection string like that above, it did not worked and I got an error of "unkown paramter inside connection string"
CONNECTION_STRING=DRIVER=libdb2o.so;Database=XXXX;Protocol=tcpip;Hostname=XXXX;Servicename=XXXX;UID=XXXX;PWD=XXXX;
Well, I did not found a simpel solution (okay I found NO solutions or explanations), therefore I would be greatful if someone know how I could fix that problem. Maybe any ideas to simpel use UTF8 inside my INSERT without changing the content of my std::string?
Am I on the wrong track that the problem could be UTF8 encoding? Any other ideas?
I have a Question. I want to copy something from a string. I have this:
string buffer = "
{"clientToken":"clientToken","accessToken":"abdjuhsdhjsksdnasfldafgkuadbkghubdfhlujgbdfhulgdfbhugfdbgujhlfdanhjgkhdfanhnjkgbafdhkugbadcjgfdabhgfdabgjhdfabkhgfdbghujfdabghkjfdabghujfadbgfdjhaugbafdhjujgjbfuhkgbf
dhugdbfauhgbfaluhgbdafilgbdfhgfdigujladbijfbghdufjbvgfbhgadbfgbdfjgfbgjfdbjflbgjedfbgauiadfbuigbuifgdabhf
juhgbdaihjfhgbdiuflghbdfiugbfdugbbbbb","selectedProfile":
{"name":"secret","id":"secret"},"availableProfiles":[{"name":"secret","id":"nothing"}]}";
string token;
and i want to put the accestoken (the long gibberish) into the string token(the rest is not needed only the accesstoken). Can anyone help me?
There are few ways to do it, but the most simple is to locate "accessToken" with std::string::find(), then located leading and ending " of the token value itself, and then use std::string::substr() to extract a necessary piece. But in general, you better use some json parsing library, like https://github.com/nlohmann/json
I'm having a problem which seems simple but I just can not get it to work. I'm using the standard C++ function append() to add BOTH a space, " ", and another one-word-string (str2) to the end of another string (str1)
My code works perfectly fine when I only append one or the other, i.e.:
str1.append(" ");
or:
str1.append(str2);
However, when I try to append both in a row as such:
str1.append(" ");
str1.append(str2);
I immediately get a segmentation error. I am very confused as to how it can handle one append, but not two! Does anyone see a work-around?
Thanks in advance!
So originally str2 was actually a double that I had stored as a string, which should still work. However for some reason C++ wouldn't use it even though it was a string. So instead I switched str2 back into a double using stod(str2), and then back into a string again at concatenation as such:
str2=stod("stuff");
str1.append(to_string(str2));
No idea why this works while the other way doesn't (both methods input a string into append()), but it works!
Is there something like fgetc in c++? File contains 3 words "send more money" and I need to solve a task where each letter represents a number(money is the answer(:D) and I add send+more), thats why I want to read each string as char array, but now I'm struggling to do so, the only info that I got is how to read everything between spaces. So, is there something like fgetc was in c or should I rethink how to do this task?
(sorry if I will be slow to respond have to go to sleep)
There's always std::istream::get(), which works exactly like
fgetc. More idiomatic in C++ might be to use
std::istream::get(char&), which stores the results directly
into a char (rather than returning an int, which must be
checked for EOF before converting it to a char), and
signals end of file or an error in the usual way.
istream/fstream have a get function that will return characters
I have a json object in c++. I am using json_cpp library.
I want to get the string from the Json::Value object. I am using it like below.
Json::Value obj;
....
....
....
string str = obj.toStyledString();
This returns the string in the pretty print format. But I want the string without any indentation. How can I do that as there are no other functions provided in the class?
You could use Json::FastWriter it does not have any indentation and formatting since it outputs everything on a single line. it is normally not suitable for 'human' consumption.
std::string toUnStyledString(const Json::Value& value)
{
Json::FastWriter writer;
return writer.write( value );
}
The function toStyledString also simply uses a Json::StyledWriter if you look into the definition of Json::Value::toStyledString.
Well, if this library doesn't provide appropriate methods then you could write them yourself. The JSON format is rather simple, so I don't think that it will take a lot of work.
Here you can find a good graphical representation of JSON format:
http://json.org
P.S. I've never worked with this particular library, so I propose sort of a general solution.
UPDATE: another option is to get a string returned by toStyledString() and remove indentation. But it requires string processing and will probably be resource consuming. Note that you can't just remove tabs/spaces/new line symbols, because they can be a part of JSON object.
Why do you want unindented string again?