The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
passing C++ variables to SQL statement
Discuss passing C++ variables to SQL statement in the C Programming forum on Dev Shed. passing C++ variables to SQL statement C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 10th, 2003, 05:09 PM
|
|
Contributing User
|
|
Join Date: Feb 2003
Location: USA
Posts: 144
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
passing C++ variables to SQL statement
Hello,
I have a recordset open function and would like to limit the returned recordset with a WHERE statement. It works without problems if the value after WHERE is just a literal string, however, that value must be a variable that will change. When I substitue with a variable, the program crashes.
How can I insert a variable there instead of using a predefined value??
Doesnt work:
CString MyVariable = "helpme";
recordset.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM MyTable WHERE NameField = MyVariable",NULL);
Does work:
recordset.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM MyTable WHERE NameField = 'helpme'",NULL);
I am using visual C++ and trying to connect to a Microsoft Access database if that makes a difference,
Thank you!!!
|

June 10th, 2003, 08:05 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
We were just talking about this:
Code:
CString MyVariable = "helpme";
recordset.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM MyTable WHERE NameField = '"+MyVariable+"'",NULL);
Now, that is assuming that recordset.Open() accepts a CString as its second parameter. If it does not, then you should create a CString (eg, MySQLstring) for the entire SQL statement, concatenate your variable value in as shown, and do whatever you need to to get Open to read it, like maybe MySQLstring.GetBuffer().
|

June 10th, 2003, 08:11 PM
|
|
Contributing User
|
|
Join Date: Feb 2003
Location: USA
Posts: 144
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
passing C++ variable to SQL success
Thank you for your reply -
I also happened upon a way to do it as follows:
CString MyVariable = "Mr. Bigglesworth";
CString sSql;
sSql.Format("SELECT * FROM NamesTable WHERE NameField = '%s' ", (LPCSTR)MyVariable);
recordset.Open(AFX_DAO_USE_DEFAULT_TYPE, sSql, NULL);
Diabolical!
Hope that helps someone down the road.
|

June 10th, 2003, 11:03 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Yep, does the same thing. Perl isn't the only language with more than one way.
Basically, CString's Format() method implements the functionality of the sprintf function. I haven't played with MFC for a while, so it slipped my mind.
Nice to see that Open takes a CString argument.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|