VC++ access MySQL query problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 8
Reputation: kent01981 is an unknown quantity at this point 
Solved Threads: 0
kent01981 kent01981 is offline Offline
Newbie Poster

VC++ access MySQL query problem

 
0
  #1
Oct 30th, 2009
VC++ access MySQL query problem
Hi guys,
Could you help me out with
------------
string str1
res=stmt->executeQuery("select * from test2 where english='str1'");
------------
anyone can tell me where is wrong with this query code?below is the error msg I got:

1>.\temp6.cpp(48) : error C2146: syntax error : missing ')' before identifier 'str1'
1>.\temp6.cpp(48) : error C2059: syntax error : ')'
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
Oct 30th, 2009
I don't see anything wrong with it -- but did you post exactly the same thing that is in your program? Maybe you need to post more of the program.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: kent01981 is an unknown quantity at this point 
Solved Threads: 0
kent01981 kent01981 is offline Offline
Newbie Poster

here is the code

 
0
  #3
Oct 30th, 2009
Originally Posted by Ancient Dragon View Post
I don't see anything wrong with it -- but did you post exactly the same thing that is in your program? Maybe you need to post more of the program.

#include <stdafx.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>


using namespace std;

int main(void)

{
string (str1);
try
{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;


/* Create a connection */
driver = get_driver_instance();
con = driver->connect("localhost", "root", "qwerty");
/* Connect to the MySQL menagerie database */
con->setSchema("menagerie");

stmt = con->createStatement();
stmt->execute("set names \'GBK\'");


cout << "Please enter an english word: ";
cin >> str1;

res=stmt->executeQuery("select * from test2 where english="str1"");----------提示错误就是说这句!但是我不知道是怎么错了?
while (res->next())
{
cout << res<<endl;

cout <<setiosflags(ios::left)<<setw(9)<<res->getString(1)<<" ";//1 is english
cout <<setiosflags(ios::left)<<setw(9)<<res->getString(2);//2 is chinese

}
delete res;
delete stmt;
delete con;
}
catch (sql::SQLException &e)
{
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}

cout << endl;

return EXIT_SUCCESS;
}[/CODE][/CODE]
Last edited by kent01981; Oct 30th, 2009 at 4:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #4
Oct 30th, 2009
>>res=stmt->executeQuery("select * from test2 where english="str1"");

That has two problems:
1) "str1": you have to escape the double quotes. \"str1\"

2) The string has to be properly formatted before sending it to executeQuery() function. C++ does not automatically replace "str1" in the string with whatever word you entered.
  1. std::string command = "select * from test2 where english=\"" + str1 + "\"";
  2. res=stmt->executeQuery(command.c_str());
Last edited by Ancient Dragon; Oct 30th, 2009 at 3:53 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: kent01981 is an unknown quantity at this point 
Solved Threads: 0
kent01981 kent01981 is offline Offline
Newbie Poster

It works!

 
0
  #5
Oct 30th, 2009
Originally Posted by Ancient Dragon View Post
>>res=stmt->executeQuery("select * from test2 where english="str1"");

That has two problems:
1) "str1": you have to escape the double quotes. \"str1\"

2) The string has to be properly formatted before sending it to executeQuery() function. C++ does not automatically replace "str1" in the string with whatever word you entered.
  1. std::string command = "select * from test2 where english=\"" + str1 + "\"";
  2. res=stmt->executeQuery(command.c_str());
It is working ! Thanks a lot Ancient Dragon!
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC