| | |
VC++ access MySQL query problem
![]() |
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
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 : ')'
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 : ')'
-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.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
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 3:02 pm.
-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.
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.
C++ Syntax (Toggle Plain Text)
std::string command = "select * from test2 where english=\"" + str1 + "\""; res=stmt->executeQuery(command.c_str());
Last edited by Ancient Dragon; Oct 30th, 2009 at 2:53 pm.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
>>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.
C++ Syntax (Toggle Plain Text)
std::string command = "select * from test2 where english=\"" + str1 + "\""; res=stmt->executeQuery(command.c_str());
![]() |
Other Threads in the C++ Forum
- Previous Thread: clear my array to calculate average and also a small question about rand
- Next Thread: Import from Excel - Save as XML
Views: 356 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c/c++ calculator char class classes client code command compile compiler constructor conversion convert count data delete dll dynamic encryption error file files fstream function functions game givemetehcodez graph gui helpwithhomework homework iamthwee input int lazy lib link linker list loop loops math matrix memory newbie number numbers object objects opengl operator output parameter pointer pointers problem program programming project python random read recursion recursive reference server simple sockets sort spoonfeeding string strings struct student studio template templates text time tree url variable vc++ vector video visual void win32 window windows winsock wordfrequency






