1、创建数据表
//测试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`))";
2、 指定与mysql处理的字符集
my.Query("set names utf8");
3、插入数据
//插入数据
{
XDATA data;
data["name"] = u8"测试的UTF中文";
cout << my.Insert(data, "t_utf8") << endl;
}
4、输出数据,
4.1、将字符集设置为GBK格式,可以直接在数据库展示;
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();
4.2、展示
4.3、将字符集设置为utf8格式
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();
4.4、输出
发现输出是乱码,
解决方法 加UTF-8转GBK的代码,代码见上篇文章
https://blog.csdn.net/wjl990316fddwjl/article/details/135417012
cout << "id: " << row[0].data << " name: " << row[1].UTF8TOGBK() << endl;