Sqlite   发布时间:2022-05-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了sqlite中文乱码问题原因分析及解决大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在VC++中通过sqlite3.dll接口对sqlite数据库进行操作,包括打开数据库,插入,@R_326_7944@,如果操作接口输入参数包含中文字符,会导致操作异常。例如调用sqlite3_open打开数据库文件,如果文件路径出现中文,就会导致打开失败。sqlite3_exec执行sql语句,如果包含中文对应字符就会变成乱码。

这是由于sqlite数据库使用的是UTF-8编码方式,而传入的字符串是ASCII编码或Unicode编码,导致字符串格式错误。解决方案是在调用sqlite接口之前,先将字符串转换成UTF-8编码,以下提供各种字符串编码转换函数。
复制代码代码如下:
//UTF-8转Unicode std::wString Utf82Unicode(const std::string& utf8String) { int wIDesize = ::MultiBytetoWIDeChar(CP_UTF8,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<wchar_t> resultString(wIDesizE); int convresult = ::MultiBytetoWIDeChar(CP_UTF8,&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,wstrcode.c_str(),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<char> resultString(ascIISizE); int convresult =::WIDeCharToMultiByte(CP_OEMCP,ascIISize,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,(char*)strascii.c_str(),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<wchar_t> resultString(wIDesizE); int convresult = MultiBytetoWIDeChar (CP_ACP,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,wIDeString.c_str(),null); if (utf8size == 0) { throw std::exception("Error in conversion."); } std::vector<char> resultString(utf8sizE); int convresult = ::WIDeCharToMultiByte(CP_UTF8,utf8size,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; }

大佬总结

以上是大佬教程为你收集整理的sqlite中文乱码问题原因分析及解决全部内容,希望文章能够帮你解决sqlite中文乱码问题原因分析及解决所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:插入