//测试UTF-8格式 指定字段name的字符集为utf8 COLLATE字符集的排序方式
sql = "CREATE TABLE IF NOT EXISTS `t_utf8` \
(`id` INT AUTO_INCREMENT , \
`name` VARCHAR(1024) CHARACTER SET utf8 COLLATE utf8_bin,\
PRIMARY KEY(`id`))";
? ? my.Query("set names utf8");
//插入数据
{
XDATA data;
data["name"] = u8"测试的UTF中文";
cout << my.Insert(data, "t_utf8") << endl;
}
cout << "===============Print gbk string========================" << endl;
//指定与mysql处理的字符集
my.Query("set names gbk");
my.Query("select *from t_utf8");
my.StoreResult();
for (;;)
{
auto row = my.FetchRow();
if (row.size() == 0)
{
/*cout << "cedw" << endl;*/
break;
}
cout << "id: " << row[0].data << " name: " << row[1].data << endl;
}
my.FreeResult();
cout << "=============Print utf8 string============" << endl;
my.Query("set names utf8");
my.Query("select *from t_utf8");
my.StoreResult();
for (;;)
{
auto row = my.FetchRow();
if (row.size() == 0)
{
/*cout << "cedw" << endl;*/
break;
}
cout << "id: " << row[0].data << " name: " << row[1].data << endl;
}
my.FreeResult();
发现输出是乱码,
解决方法 加UTF-8转GBK的代码,代码见上篇文章
https://blog.csdn.net/wjl990316fddwjl/article/details/135417012
cout << "id: " << row[0].data << " name: " << row[1].UTF8TOGBK() << endl;