和mysql connector/c++相比,只要在开始时设置好字符集,之后不需要进行转码操作。
conn.set_option(new mysqlpp::SetCharsetNameOption("gbk"));
//查询方法1
vector<mysqlpp::Row> v;
query.storein(v);
cout.setf(ios::left);
cout<<setw(10)<<"username"<<setw(10)<<"password"<<endl;
for (vector<mysqlpp::Row>::iterator it = v.begin(); it != v.end(); ++it) {
cout<<setw(10)<<it->at(0)<<setw(10)<<it->at(1)<< endl;
}
//查询方法2
mysqlpp::StoreQueryResult res = query.store();
if(res){
cout.setf(ios::left);
cout<<setw(10)<<"username"<<setw(10)<<"password"<<endl;
for (size_t i = 0; i < res.num_rows(); ++i)
cout << setw(10) << res[i]["username"] << setw(10) << res[i]["password"] << endl;
}else {
cerr << "Failed to get stock table: " << query.error() << endl;
return 1;
}
//修改记录
query<<"update users set password='hello' where username='沈健'";
query.execute();
//插入
query<<"insert into users(username,password) values('飞翔','123456')";
query.execute();
//删除记录
query<<"delete from users where username='遨游'";
query.execute();
没有评论:
发表评论