查了半天,都快绝望了,终于解决了。
问题是程序插入的中文在数据库里显示为乱码,程序读出来仍然是中文。
很简单。
程序代码里面,链接数据库之后,插入数据之前,加入执行一条sql语句 SET NAMES utf8,形如:
db_kop.modify_db("SET NAMES utf8");
然后再操作就好了。
#include#include #include #include "db_helper.h"using std::cout;using std::endl;int main(int argc, char* argv[]){ static db_helper db_kop; db_kop.connect_db("127.0.0.1", "root", "cptbtptp"); db_kop.modify_db("SET NAMES utf8"); db_kop.modify_db("INSERT INTO `kop_fnic`.`kop_rule`(`rid`, `protocol`, `app`, `behav`, `os`, `agent`, `cat`) VALUES ('','a','0','新','浪','微','博')"); db_kop.select_db("SELECT * FROM `kop_fnic`.`kop_rule` LIMIT 0 , 30"); while (char** r = db_kop.fetch_row()) { printf("%s %s %s %s %s %s\n", r[1], r[2], r[3], r[4], r[5], r[6]); } db_kop.modify_db("INSERT INTO `kop_fnic`.`kop_rule`(`rid`, `protocol`, `app`, `behav`, `os`, `agent`, `cat`) VALUES ('','a','1','新','浪','微','博')"); db_kop.modify_db("INSERT INTO `kop_fnic`.`kop_rule`(`rid`, `protocol`, `app`, `behav`, `os`, `agent`, `cat`) VALUES ('','a','2','新','浪','微','博')");// db_kop.modify_db("SET NAMES utf8"); sleep(1); db_kop.modify_db("INSERT INTO `kop_fnic`.`kop_rule`(`rid`, `protocol`, `app`, `behav`, `os`, `agent`, `cat`) VALUES ('','a','3','新','浪','微','博')"); db_kop.select_db("SELECT * FROM `kop_fnic`.`kop_rule` LIMIT 0 , 30"); while (char** r = db_kop.fetch_row()) { printf("%s %s %s %s %s %s\n", r[1], r[2], r[3], r[4], r[5], r[6]); } return 0;}