CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2003
    Posts
    77

    a very basic question

    the following is a bit of the code iam using! to access a database.
    ConStr[0] = '\0';
    strcat(ConStr, "SELECT * FROM prodlist WHERE product_name = 'aegis 1.0'");
    pRecordset = m_pConnection->Execute(ConStr, vRecordsAffected, 1);

    Now I want to assign it to another variable and then use the variable in place of 'aegis 1.0'.
    to what variable should i assign it??
    sriranjani

  2. #2
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    String manipulation

    SJ,

    Are you using the C++ language? If so, then I recommend using standard template library strings (STL strings) for these kinds of string manipulations.

    The STL strings make it very easy to manipulate strings.

    Look at the following example:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
      string control   = "SELECT * FROM prodlist WHERE product_name = ";
      string selection = "aegis 1.0";
    
      cout << control + selection << endl;
    
      return 0;
    }
    Good luck.

    Sincerely, Chris.

    You're gonna go blind staring into that box all day.

  3. #3
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    You are much better off using stringstreams:

    std::stringstream query;

    query << "SELECT * FROM [prodlist] WHERE [product_name] = '" << variable << "';";

    pRecordset = m_pConnection->Execute( query.str().c_str(), vRecordsAffected, 1 );

  4. #4
    Join Date
    Jun 2003
    Location
    India Trichy
    Posts
    245
    strcpy(ConStr, "SELECT * FROM prodlist WHERE product_name=");
    strcat(ConStr, "'aegis 1.0'")
    pRecordset = m_pConnection->Execute(ConStr, vRecordsAffected, 1);

    strcpy(ConStr, "SELECT * FROM prodlist WHERE product_name=");
    (ConStr, "'aegis 2.0'");//change Here ..
    pRecordset = m_pConnection->Execute(ConStr, vRecordsAffected, 1);

    OR

    sprintf(ConStr, "SELECT * FROM prodlist WHERE product_name ='%s' ","aegis 2.0"); /// We can change Here ...
    pRecordset = m_pConnection->Execute(ConStr, vRecordsAffected, 1);

    Regards
    Balamurali C
    Last edited by ccbalamurali; October 31st, 2003 at 08:21 AM.

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