首页 技术 正文
技术 2022年11月11日
0 收藏 777 点赞 2,907 浏览 2708 个字

我参考的是这篇文章:https://www.2cto.com/database/201411/354891.html

理论是:sqlite使用的是UTF-8,C++中用的字符串是ascii或unicode编码。

所以使用时候要进行转化。插入中文时候要转化为UTF-8,读取时候再转化回来。

下面是转化函数,参考文章:https://www.2cto.com/database/201411/354891.html

    //UTF-8转Unicode
std::wstring Utf82Unicode(const std::string& utf8string) {
int widesize = ::MultiByteToWideChar(CP_UTF8, , utf8string.c_str(), -, NULL, );
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (widesize == )
{
throw std::exception("Error in conversion.");
}
std::vector<wchar_t> resultstring(widesize);
int convresult = ::MultiByteToWideChar(CP_UTF8, , utf8string.c_str(), -, &resultstring[], widesize);
if (convresult != widesize)
{
throw std::exception("La falla!");
}
return std::wstring(&resultstring[]);
} //unicode 转为 ascii
std::string WideByte2Acsi(std::wstring& wstrcode){
int asciisize = ::WideCharToMultiByte(CP_OEMCP, , wstrcode.c_str(), -, NULL, , NULL, NULL);
if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (asciisize == )
{
throw std::exception("Error in conversion.");
}
std::vector<char> resultstring(asciisize);
int convresult = ::WideCharToMultiByte(CP_OEMCP, , wstrcode.c_str(), -, &resultstring[], asciisize, NULL, NULL);
if (convresult != asciisize)
{
throw std::exception("La falla!");
}
return std::string(&resultstring[]);
} //utf-8 转 ascii
std::string UTF_82ASCII(std::string& strUtf8Code){
using namespace std;
string strRet = "";
//先把 utf8 转为 unicode
wstring wstr = Utf82Unicode(strUtf8Code);
//最后把 unicode 转为 ascii
strRet = WideByte2Acsi(wstr);
return strRet;
} //ascii 转 Unicode
std::wstring Acsi2WideByte(std::string& strascii){
using namespace std;
int widesize = MultiByteToWideChar(CP_ACP, , (char*)strascii.c_str(), -, NULL, );
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (widesize == )
{
throw std::exception("Error in conversion.");
}
std::vector<wchar_t> resultstring(widesize);
int convresult = MultiByteToWideChar(CP_ACP, , (char*)strascii.c_str(), -, &resultstring[], widesize);
if (convresult != widesize)
{
throw std::exception("La falla!");
}
return std::wstring(&resultstring[]);
} //Unicode 转 Utf8
std::string Unicode2Utf8(const std::wstring& widestring){
using namespace std;
int utf8size = ::WideCharToMultiByte(CP_UTF8, , widestring.c_str(), -, NULL, , NULL, NULL);
if (utf8size == )
{
throw std::exception("Error in conversion.");
}
std::vector<char> resultstring(utf8size);
int convresult = ::WideCharToMultiByte(CP_UTF8, , widestring.c_str(), -, &resultstring[], utf8size, NULL, NULL);
if (convresult != utf8size)
{
throw std::exception("La falla!");
}
return std::string(&resultstring[]);
} //ascii 转 Utf8
std::string ASCII2UTF_8(std::string& strAsciiCode) {
using namespace std;
string strRet("");
//先把 ascii 转为 unicode
wstring wstr = Acsi2WideByte(strAsciiCode);
//最后把 unicode 转为 utf8
strRet = Unicode2Utf8(wstr);
return strRet;
}

使用效果立竿见影:

解决C++项目使用sqlite中文乱码问题

可以看出,之前存进数据库的是乱码

改变之后已经成功将汉字存进去了

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848