Solved

How do I build SQL "Insert into" basic_stringstream using variables in C++

Posted on 2004-09-17
7
358 Views
Last Modified: 2012-06-22
I'm trying to build a function that will take string inputs and build an SQL statement for input into a database.  Here's my existing code:

void Connector::InsertData(string _table, string _column, string _value)
{
        // string I/O stream
    basic_stringstream<wchar_t> strSQLStatement;
    variant_t vntRecordAffected;

      strSQLStatement <<L"INSERT INTO " << _table.c_str() << "(" << _column.c_str() << ") VALUES('" << _value.c_str()<< "' ) ";

    cout << "strSQLStatement =" << strSQLStatement.str().c_str() <<endl;
    _bstr_t strSQLstmt = strSQLStatement.str().c_str();

    try
    {
        _connect->Execute(strSQLstmt, &vntRecordAffected, adCmdText );
    }
    catch( _com_error &e)
    {
        // get info from _com_error
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());

        printf("%s", e.ErrorMessage());
        cout << endl << "No Data Entered." << endl << endl;
    }  // end catch

      cout << "Data Inserted." << endl;

}  //End InsertData

Does anybody have an input?
0
Comment
Question by:RedHedToo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 12091216
Use wcout to see the wide string:

i.e.

wcout << "strSQLStatement =" << strSQLStatement.str().c_str() <<endl;
0
 

Author Comment

by:RedHedToo
ID: 12101904
Sorry, I should've looked closer for editing purposes prior to putting it up.  The problem is that the string always sends back an error.  I've tried several ways to build a string to create the statement, but the only thing that works so far is creating an entire string at one time without actually using any data in variables.  The line I have in there now:

     strSQLStatement <<L"INSERT INTO " << _table.c_str() << "(" << _column.c_str() << ") VALUES('" << _value.c_str()<< "' ) ";

doesn't actually work properly.

Anybody have a solution?

Red.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12102092
If I hack your code thus, I get what looks like good SQL:
--------8<--------
#include <iostream>
#include <sstream>
using namespace std;

void /*Connector::*/InsertData(string _table, string _column, string _value)
{
        // string I/O stream
    basic_stringstream<wchar_t> strSQLStatement;
//    variant_t vntRecordAffected;

     strSQLStatement <<L"INSERT INTO " << _table.c_str() << "(" << _column.c_str() << ") VALUES('" << _value.c_str()<< "' ) ";

    wcout << "strSQLStatement =" << strSQLStatement.str().c_str() <<endl;
}

int main()
{
      InsertData("mytable","mycolumn","myvalue");
}
--------8<--------

Output is:

strSQLStatement =INSERT INTO mytable(mycolumn) VALUES('myvalue' )

My SQL experience isn't strong enough to vouch whether you can get away with the lack of space between mytable and '('.

Is this not what you are getting?

Clearly this is only good for a table which has one field in it only. You have put quotes around myvalue, which means you couldn't conveniently pass a comma separated strings. You'd need to have something like

      InsertData("mytable"
            ,"my1stcolumn,my2ndcolumn"
            ,"my1stvalue','my2ndValue"      /* <- Weird quotes! */
      );
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RedHedToo
ID: 12102383
I'm getting an error, actually.  "IDispatch error #3092"  When I googled it , the Microsoft site said I needed single quotes, which I already have.  It also said it may be a problem with MDAC 2.5 (I'm running 2.7), so right now I don't know the exact problem.  

Red.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12103665
I recommend you close this question (ask for a refund) and start a new question with an appropriate title (e.g. "MS MSL - IDispatch error #3092"). Some folks who are knowledgeable about MS SQL may not be looking at this, thinking this is an STL problem, which it isn't.
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 135 total points
ID: 12305971
Delete question. The title was misleading. The problem wasn't handled by my answers, because it was unrelated.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 130
Error creating a new C++ project in ,net 20 60
draw a Christmas tree by using a nested loop? 26 106
How do I get Window Title of all opened process? 4 61
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question