下面的例子是MYSQL++自带的例子
我的表里面有中文的数据
程序输出的全是问号
如何解决,谢谢~!
int main(int argc, char *argv[])
{
// Get database access parameters from command line
const char* db = "bbs", *server = "localhost", *user = "root", *pass = "admin";
// Connect to the sample database.
mysqlpp::Connection conn(false);
if (conn.connect(db, server, user, pass)) {
// Retrieve a subset of the sample stock table set up by resetdb
// and display it.
mysqlpp::Query query(&conn,false,"select topic from codes");
if (mysqlpp::StoreQueryResult res = query.store()) {
cout << "We have:" << endl;
for (size_t i = 0; i < res.num_rows(); ++i) {
cout << '\t' << res[i][0] << endl;
}
}
else {
cerr << "Failed to get item list: " << query.error() << endl;
return 1;
} return 0;
}
else {
cerr << "DB connection failed: " << conn.error() << endl;
return 1;
}
}
我的表里面有中文的数据
程序输出的全是问号
如何解决,谢谢~!
int main(int argc, char *argv[])
{
// Get database access parameters from command line
const char* db = "bbs", *server = "localhost", *user = "root", *pass = "admin";
// Connect to the sample database.
mysqlpp::Connection conn(false);
if (conn.connect(db, server, user, pass)) {
// Retrieve a subset of the sample stock table set up by resetdb
// and display it.
mysqlpp::Query query(&conn,false,"select topic from codes");
if (mysqlpp::StoreQueryResult res = query.store()) {
cout << "We have:" << endl;
for (size_t i = 0; i < res.num_rows(); ++i) {
cout << '\t' << res[i][0] << endl;
}
}
else {
cerr << "Failed to get item list: " << query.error() << endl;
return 1;
} return 0;
}
else {
cerr << "DB connection failed: " << conn.error() << endl;
return 1;
}
}
lpha, for Win32 (ia32)Connection id: 15
Current database:
Current user: root@localhost
SSL: Not in use
Using delimiter: ;
Server version: 6.0.4-alpha-community-log MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: gb2312
Db characterset: gb2312
Client characterset: gb2312
Conn. characterset: gb2312
TCP port: 3306
Uptime: 1 hour 6 min 27 secThreads: 1 Questions: 31 Slow queries: 0 Opens: 16 Flush tables: 1 Open tab
les: 0 Queries per second avg: 0.7
--------------mysql>
mysql_set_character_set(&mysql, "GBK"))[align=center]==== 思想重于技巧 ====
[/align]
mysqlpp::Connection conn(false);
conn.set_option(new mysqlpp::SetCharsetNameOption("GBK"));
if (conn.connect(db, server, user, pass,nPort))
...
注意需要在连接之前设置