CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2009
    Posts
    20

    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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: SQL statement with MFC

    What do you mean "attach 2 variables"?

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: SQL statement with MFC

    Quote Originally Posted by JohnW@Wessex View Post
    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.

  5. #5
    Join Date
    Jan 2009
    Posts
    20

    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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: SQL statement with MFC

    Quote Originally Posted by hijjawi View Post
    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);

  7. #7
    Join Date
    Jan 2009
    Posts
    20

    Talking 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
  •  





Click Here to Expand Forum to Full Width

Featured