Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC8.0
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.
 
CString SqlStr4;
SqlStr4 = "INSERT INTO log SET grab_date= '";
SqlStr4 +=strDateTime;
SqlStr4 += "',ip_address='";
SqlStr4 +=SqlStr3;
SqlStr4 += "',image='";
SqlStr4 += StrByte;
SqlStr4 +="';";
Posted 4-May-11 3:22am
Edited 4-May-11 3:45am
v4
Rate this: bad
good
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...
  Permalink  
v2
Comments
Gokulnath007 at 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 at 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 at 4-May-11 8:40am
   
No, SqlStr3 just stores the value 6. Am not using any thread.
Chuck O'Toole at 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 at 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 at 4-May-11 9:29am
   
CString strDateTime;
SYSTEMTIME datetime;
::GetLocalTime(&datetime);
strDateTime.Format(_T("%02i-%02i-%02i %d:%d:%d"),
datetime.wYear,
datetime.wMonth,
datetime.wDay,
datetime.wHour,
datetime.wMinute,
datetime.wSecond);
 

CRecordset rs(db);
rs.Open(CRecordset::snapshot,strQuery,CRecordset::readOnly);
CDBVariant ip;
rs.GetFieldValue(short(0),ip);
CString strip;
for(int i=0;iGetLength();i++)
{
strip+=ip.m_pstringA->GetAt(i);
}

strip.TrimRight();
 
CString SqlStr3(strip);
Gokulnath007 at 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 at 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 at 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 at 5-May-11 4:34am
   
Good point. It deserves a try.
Rate this: bad
good
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?
  Permalink  
Rate this: bad
good
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.
  Permalink  
Comments
Chuck O'Toole at 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
good
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.
Regards.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 329
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 185
4 PIEBALDconsult 149
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 4 May 2011
Copyright © CodeProject, 1999-2014
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