您好,欢迎来到爱问旅游网。
搜索
您的当前位置:首页解决SQLite数据库中文乱码问题

解决SQLite数据库中文乱码问题

来源:爱问旅游网
解决SQLite数据库中⽂乱码问题

关于SQLite中出现中⽂乱码的分析以及解决⽅案

我们在使⽤SQLite数据库时候,可能会发现,向数据库插⼊数据时候显⽰的是汉字,但通过SQLite读出来时却显⽰的乱码,这是因为SQLite数据库所⽀持的编码⽅式和我们程序中的编码⽅式不⼀样,SQLite数据库采⽤的是UTF-8编码⽅式,⽽我们在程序中常常使⽤的是宽字节uncoid编码⽅式,所以使⽤SQLite数据库读出来以后会显⽰乱码,就是因为编码⽅式不⼀样,举个例⼦,基于对话框的程序,我们要在listctrl控件上显⽰我们数据库中读⼊的数据,当我们编写程序让读出来时,列表控件上显⽰的却是不可读的乱码,这就是原因所在。

例如在VC++中通过sqlite3.dll接⼝对sqlite数据库进⾏操作,包括打开数据库,插⼊,查询数据库等,如果操作接⼝输⼊参数包含中⽂字符,会导致操作异常。例如调⽤sqlite3_open打开数据库⽂件,如果⽂件路径出现中⽂,就会导致打开失败。sqlite3_exec执⾏sql语句,如果包含中⽂对应字符就会变成乱码。

有问题肯定是要解决滴,本⼈初学SQLite,通过查找资料,这是由于sqlite数据库使⽤的是UTF-8编码⽅式,⽽传⼊的字符串是ASCII编码或Unicode编码,导致字符串格式错误。解决⽅案是在调⽤sqlite接⼝之前,先将字符串转换成UTF-8编码,以下提供各种字符串编码转换函数。。函数如下:

⼀般我们使⽤vc++编程时最多使⽤的函数是UTF-8转Unicode ,⼀下函数仅供参考

//UTF-8转Unicode

std::wstring Utf82Unicode(const std::string& utf8string) {

int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0); if (widesize == ERROR_NO_UNICODE_TRANSLATION) {

throw std::exception(\"Invalid UTF-8 sequence.\"); }

if (widesize == 0) {

throw std::exception(\"Error in conversion.\"); }

std::vector resultstring(widesize);

int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize); if (convresult != widesize) {

throw std::exception(\"La falla!\"); }

return std::wstring(&resultstring[0]); }

//unicode 转为 ascii

string WideByte2Acsi(wstring& wstrcode) {

int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL); if (asciisize == ERROR_NO_UNICODE_TRANSLATION) {

throw std::exception(\"Invalid UTF-8 sequence.\"); }

if (asciisize == 0) {

throw std::exception(\"Error in conversion.\"); }

std::vector resultstring(asciisize);

int convresult =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL); if (convresult != asciisize) {

throw std::exception(\"La falla!\"); }

return std::string(&resultstring[0]); }

//utf-8 转 ascii

string UTF_82ASCII(string& strUtf8Code) {

string strRet(\"\");

//先把 utf8 转为 unicode

wstring wstr = Utf82Unicode(strUtf8Code); //最后把 unicode 转为 ascii strRet = WideByte2Acsi(wstr); return strRet;

}

///////////////////////////////////////////////////////////////////////

//ascii 转 Unicode

wstring Acsi2WideByte(string& strascii) {

int widesize = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0); if (widesize == ERROR_NO_UNICODE_TRANSLATION) {

throw std::exception(\"Invalid UTF-8 sequence.\"); }

if (widesize == 0) {

throw std::exception(\"Error in conversion.\"); }

std::vector resultstring(widesize);

int convresult = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize); if (convresult != widesize) {

throw std::exception(\"La falla!\"); }

return std::wstring(&resultstring[0]); }

//Unicode 转 Utf8

std::string Unicode2Utf8(const std::wstring& widestring) {

int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL); if (utf8size == 0) {

throw std::exception(\"Error in conversion.\"); }

std::vector resultstring(utf8size);

int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL); if (convresult != utf8size) {

throw std::exception(\"La falla!\"); }

return std::string(&resultstring[0]); }

//ascii 转 Utf8

string ASCII2UTF_8(string& strAsciiCode) {

string strRet(\"\");

//先把 ascii 转为 unicode

wstring wstr = Acsi2WideByte(strAsciiCode); //最后把 unicode 转为 utf8 strRet = Unicode2Utf8(wstr); return strRet; }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务