| | |
VC++ access MySQL query problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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 : ')'
•
•
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 4: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 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.
•
•
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: replacement of getch()
- Next Thread: Import from Excel - Save as XML
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






