[C++] 读取mysql数据库的问题,请各位大侠帮看看...
我是C++新手,正做一个中英文翻译的小程序,但是总是有错误,还请各位高手帮看看,下面有错误提示。谢谢了!!!
---------------------------------------------------------------------------------------------
1>------ Build started: Project: temp6, Configuration: Release Win32 ------
1>Compiling...
1>temp6.cpp
1>.\temp6.cpp(48) : error C2146: syntax error : missing ')' before identifier 'str1'
1>.\temp6.cpp(48) : error C2059: syntax error : ')'
1>Build log was saved at "file://c:\Documents and Settings\Administrator\桌面\My_Project\temp6\temp6\Release\BuildLog.htm"
1>temp6 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#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;
}