Click here to Skip to main content
13,097,012 members (81,239 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am using the following code to insert the values into the MYSQL database. In this CString SqlStr4 is accepting and taking values up to sqlstr3 assumption, after that it is not taking the other values. What is the problem in this?? help me.

<code>CString SqlStr4;
SqlStr4 = "INSERT INTO log SET grab_date= '";
SqlStr4 +=strDateTime;
SqlStr4 += "',ip_address='";
SqlStr4 +=SqlStr3;
SqlStr4 += "',image='";
SqlStr4 += StrByte;
SqlStr4 +="';";</code>
Posted 4-May-11 2:22am
Updated 4-May-11 2:45am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Check that SqlStr3 is a valid string.
For example a common way to "break" a CString object is to use its GetBuffer method, modify the string, and forget to call ReleaseBuffer...

I don't understand what might be wrong. Try to comment a few lines to know exactly which one raises the problem. For example:
CString SqlStr4;
SqlStr4 = "INSERT INTO log SET grab_date= '";
SqlStr4 +=strDateTime;
SqlStr4 += "',ip_address='";
//SqlStr4 +=SqlStr3;
SqlStr4 += "',image='";
SqlStr4 += StrByte;
SqlStr4 +="';";

Once you found which lines (it might be several ones) causes the problem, do the same thing with that string. For example, if you found out that only SqlStr3 causes the problem, then look carefully how SqlStr3 is built. Try to build it a different way...
Gokulnath007 4-May-11 8:31am
Am not using GetBuffer method, all the variables having the correct values but it is not being taken into the Sqlstr4. I mean copying.
Olivier Levrey 4-May-11 8:38am
So there is something else you are not telling or showing in your code because I don't see any reason why the concatenation would fail here.
Are your using threads? Is SqlStr3 a shared variable?...
Gokulnath007 4-May-11 8:40am
No, SqlStr3 just stores the value 6. Am not using any thread.
Chuck O'Toole 4-May-11 21:48pm
Having looked at your shown output, "6" is terrible as an IP address so I don't think you're pulling the IP field out of the record set correctly. Have you looked at the varient variable with the debugger?
Chuck O'Toole 4-May-11 9:01am
Please show us values of strDateTime, SqlStr3, and StrByte as well as the final content of SqlStr4 so we can have a clue. And don't just say "stores the value 6", show the actual string value, it may not contain what you think it does.
Gokulnath007 4-May-11 9:29am
CString strDateTime;
SYSTEMTIME datetime;
strDateTime.Format(_T("%02i-%02i-%02i %d:%d:%d"),

CRecordset rs(db);
CDBVariant ip;
CString strip;
for(int i=0;i<ip.m_pstringa->GetLength();i++)


CString SqlStr3(strip);
Gokulnath007 4-May-11 9:30am
StrSql4 contains INSERT INTO log SET grab_date= '2011-05-04 18:54:49',ip_address='6 this only. Remaining is not there in it.
Olivier Levrey 4-May-11 9:56am
I don't understand where the problem comes from but I gave you a piece of advice: see my uptaded answer. I hope this will help.
Stefan63 5-May-11 4:20am
My guess is that ip.m_pstringA->GetAt(i) does not give you a char, but a wchar. I'm not at all sure what CString::operator+=() would do with an argument of type wchar, but my guess is it gets implicitely converted into something that results in nonprintable characters. Try casting to (char) first.
Olivier Levrey 5-May-11 4:34am
Good point. It deserves a try.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

What do you mean by 'does not accept'? Do you mean you get a runtime error while building SqlStr4, or does your database complain about the string you pass? If the latter, is ip_address actually a string type? maybe you should skip the quotes on this field?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

This is just a hunch, but is there any chance that SqlStr3 is a unicode string? The reason I ask is that it appears that your other strings are not unicode and perhaps that is causing your problem.

Just a thought.
Chuck O'Toole 4-May-11 21:43pm
Yes, it seems there's a NULL character (\0) in SqlStr3. I'm not sure how it got there, UNICODE would be one way, the other is this loop he used "for(int i=0;iGetLength();i++)". His post did not show the relationship for the terminating condition but it could also pick up a NULL if it's anything other then "<".
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

You have to use the mysql_escape_string function to make a valid call.
So you have to escape your strings strDateTime, SqlStr3, StrByte.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 4 May 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100