C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 10th, 2003, 05:09 PM
theRaginCajun theRaginCajun is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: USA
Posts: 144 theRaginCajun User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!!!

Reply With Quote
  #2  
Old June 10th, 2003, 08:05 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 15 h 6 m 34 sec
Reputation Power: 1949
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().

Reply With Quote
  #3  
Old June 10th, 2003, 08:11 PM
theRaginCajun theRaginCajun is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: USA
Posts: 144 theRaginCajun User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old June 10th, 2003, 11:03 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 15 h 6 m 34 sec
Reputation Power: 1949
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > passing C++ variables to SQL statement

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap