您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/5/4 20:56:06发布35次查看
关于sqlite中出现中文乱码的分析以及解决方案 我们在使用sqlite数据库时候,可能会发现,向数据库插入数据时候显示的是汉字,但通过sqlite读出来时却显示的乱码,这是因为sqlite数据库所支持的编码方式和我们程序中的编码方式不一样,sqlite数据库采用的是ut
关于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; }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product