Thread: string variable in C++ to be used qith odbc sql insert

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    12

    string variable in C++ to be used qith odbc sql insert

    Hello everyone,
    im trying to use an sql insert into a C++ application but unfortunately i cant input the values of the string variables to the tables!
    below is an example of my insert statement, the same statement works fine with integers, etc but not with string variables!
    Code:
    sprintf((char*)SQLStmt,"INSERT INTO teststr(thestr) VALUES ('%s');",str);
    if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle,SQLStmt, SQL_NTS)){
        show_error(SQL_HANDLE_STMT, sqlstatementhandle);  
    
        }
    can you help please?
    thanx

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Try
    Code:
    sprintf((char*)SQLStmt,"INSERT INTO teststr(thestr) VALUES ('%s');",str.c_str());
    Kurt

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You should be using Prepared Statements to prevent SQL Injection attacks.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    12
    Quote Originally Posted by ZuK View Post
    Try
    Code:
    sprintf((char*)SQLStmt,"INSERT INTO teststr(thestr) VALUES ('%s');",str.c_str());
    Kurt
    the str variable it is a string. ex. string str="hello";

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    12
    Quote Originally Posted by ZuK View Post
    Try
    Code:
    sprintf((char*)SQLStmt,"INSERT INTO teststr(thestr) VALUES ('%s');",str.c_str());
    Kurt
    I made some changes to the RETCODE variable and it worked. But it had a problem and it wasnt able to insert the proper characters of a string variable. After i added '.c_str()', everything was just fine! Thanx Kurt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. insert a char anywhere in a string
    By panosstylianou in forum C Programming
    Replies: 3
    Last Post: 12-15-2011, 08:49 PM
  2. Insert a character in a string
    By madmars in forum C Programming
    Replies: 9
    Last Post: 02-09-2011, 02:57 PM
  3. Insert int into string
    By lpmrhahn in forum C Programming
    Replies: 1
    Last Post: 04-09-2004, 02:10 PM
  4. Unable to insert a string into another string
    By Jacquiline in forum C Programming
    Replies: 0
    Last Post: 04-05-2003, 09:15 AM
  5. compiling qith RHIDE
    By Robert_Ingleby in forum C Programming
    Replies: 1
    Last Post: 03-01-2002, 08:11 PM