|
-
June 11th, 2009, 07:31 AM
#1
SQL statement with MFC
Good Day All,
Really I tried to retrieve data from my Access DB but I struggle with the syntax when I need to attach an variables with the SELECT statement like the following :
CString X("2");
CString SQL1;
SQL= "SELECT Col1 from Table where Col2="+X; // its works fine no problems although that its declared as CString and Col2 is integer in the DB
but my struggling when I want to attach 2 variables I tried many times and I have different types of errors somtimes from the compiler and sometimes from the ACCESS "there is missing in your expression"
Please any one can help me and I really appreciate that
Hijjawi
-
June 11th, 2009, 07:45 AM
#2
Re: SQL statement with MFC
What do you mean "attach 2 variables"?
-
June 11th, 2009, 07:55 AM
#3
Re: SQL statement with MFC
Code:
CString X("2");
CString SQL1;
SQL1= "SELECT Col1 from Table where Col2="+X;
This doesn't compile for me (after correcting the typo)
Try this (for two variables)
Code:
CString X("2");
CString Y("3");
CString SQL1;
SQL1 = CString("SELECT Col1 from Table where Col2=") + X + Y;
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
June 11th, 2009, 08:10 AM
#4
Re: SQL statement with MFC
 Originally Posted by JohnW@Wessex
Code:
CString X("2");
CString SQL1;
SQL1= "SELECT Col1 from Table where Col2="+X;
This doesn't compile for me (after correcting the typo)
Try this (for two variables)
Code:
CString X("2");
CString Y("3");
CString SQL1;
SQL1 = CString("SELECT Col1 from Table where Col2=") + X + Y;
That would give him Col2 = 23. I don't think that's what he means.
-
June 11th, 2009, 08:32 AM
#5
Re: SQL statement with MFC
Thanks for all,
But I mean I want to attach 2 variables to match them with two column's values like:
CString SQL = " SELECT Col1 from Table where Col2= Variable1 and Col2= Variable2 "
My tries were:
CString SQL = " SELECT Col1 from Table where Col2="+X AND Col3="+Y "; // failed
CString SQL = " SELECT Col1 from Table where Col2=+X AND Col3=+Y "; // failed
CString SQL = " SELECT Col1 from Table where Col2='+X' "AND" Col3='+Y' "; // failed
CString SQL = " SELECT Col1 from Table where Col2= AND Col3="+X +Y "; // failed
CString SQL = " SELECT Col1 from Table where Col2=' "+X+" ' AND Col3=' "+Y+ " ' "; // failed
I think my problem with the syntax code not anything else.
Any help please
-
June 11th, 2009, 08:43 AM
#6
Re: SQL statement with MFC
 Originally Posted by hijjawi
Thanks for all,
But I mean I want to attach 2 variables to match them with two column's values like:
CString SQL = " SELECT Col1 from Table where Col2= Variable1 and Col2= Variable2 "
My tries were:
CString SQL = " SELECT Col1 from Table where Col2="+X AND Col3="+Y "; // failed
CString SQL = " SELECT Col1 from Table where Col2=+X AND Col3=+Y "; // failed
CString SQL = " SELECT Col1 from Table where Col2='+X' "AND" Col3='+Y' "; // failed
CString SQL = " SELECT Col1 from Table where Col2= AND Col3="+X +Y "; // failed
CString SQL = " SELECT Col1 from Table where Col2=' "+X+" ' AND Col3=' "+Y+ " ' "; // failed
I think my problem with the syntax code not anything else.
Any help please
CString SQL;
SQL.Format("SELECT Col1 from table where Col2 = %d AND Col3 = %d", x, y);
-
June 11th, 2009, 09:16 AM
#7
Re: SQL statement with MFC
Dear GCDEF,
Thank you very much its work now .
thanks alot and best regards
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|