sqlite中文乱码问题解决方案

在VC++中通过sqlite3.dll接口对sqlite数据库进行操作,包括打开数据库,插入,查询数据库等,如果操作接口输入参数包含中文字符,会导致操作异常。例如调用sqlite3_open打开数据库文件,如果文件路径出现中文,就会导致打开失败。sqlite3_exec执行sql语句,如果包含中文对应字符就会变成乱码。这是由于sqlite数据库使用的是UTF-8编码方式,而传入的字符串是ASCII编码或Unicode编码,导致字符串格式错误。解决方案是在调用sqlite接口之前,先将字符串转换成UTF-8编码,以下提供各种字符串编码转换函数。

[cpp]  view plain copy
  1. //UTF-8转Unicode  
  2. std::wstring Utf82Unicode(const std::string& utf8string)  
  3. {  
  4.     int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);  
  5.     if (widesize == ERROR_NO_UNICODE_TRANSLATION)  
  6.     {  
  7.         throw std::exception("Invalid UTF-8 sequence.");  
  8.     }  
  9.     if (widesize == 0)  
  10.     {  
  11.         throw std::exception("Error in conversion.");  
  12.     }  
  13.   
  14.     std::vector<wchar_t> resultstring(widesize);  
  15.   
  16.     int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);  
  17.   
  18.     if (convresult != widesize)  
  19.     {  
  20.         throw std::exception("La falla!");  
  21.     }  
  22.   
  23.     return std::wstring(&resultstring[0]);  
  24. }  
  25.   
  26. //unicode 转为 ascii  
  27.   
  28. string WideByte2Acsi(wstring& wstrcode)  
  29. {  
  30.     int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);  
  31.     if (asciisize == ERROR_NO_UNICODE_TRANSLATION)  
  32.     {  
  33.         throw std::exception("Invalid UTF-8 sequence.");  
  34.     }  
  35.     if (asciisize == 0)  
  36.     {  
  37.         throw std::exception("Error in conversion.");  
  38.     }  
  39.     std::vector<char> resultstring(asciisize);  
  40.     int convresult =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);  
  41.   
  42.     if (convresult != asciisize)  
  43.     {  
  44.         throw std::exception("La falla!");  
  45.     }  
  46.   
  47.     return std::string(&resultstring[0]);  
  48. }  
  49.   
  50. //utf-8 转 ascii  
  51.   
  52. string UTF_82ASCII(string& strUtf8Code)  
  53. {  
  54.     string strRet("");  
  55.   
  56.   
  57.     //先把 utf8 转为 unicode   
  58.     wstring wstr = Utf82Unicode(strUtf8Code);  
  59.   
  60.     //最后把 unicode 转为 ascii  
  61.     strRet = WideByte2Acsi(wstr);  
  62.   
  63.   
  64.     return strRet;  
  65. }  
  66.   
  67. ///  
  68.   
  69. //ascii 转 Unicode  
  70.   
  71. wstring Acsi2WideByte(string& strascii)  
  72. {  
  73.     int widesize = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);  
  74.     if (widesize == ERROR_NO_UNICODE_TRANSLATION)  
  75.     {  
  76.         throw std::exception("Invalid UTF-8 sequence.");  
  77.     }  
  78.     if (widesize == 0)  
  79.     {  
  80.         throw std::exception("Error in conversion.");  
  81.     }  
  82.     std::vector<wchar_t> resultstring(widesize);  
  83.     int convresult = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);  
  84.   
  85.     if (convresult != widesize)  
  86.     {  
  87.         throw std::exception("La falla!");  
  88.     }  
  89.   
  90.     return std::wstring(&resultstring[0]);  
  91. }  
  92.   
  93. //Unicode 转 Utf8  
  94.   
  95. std::string Unicode2Utf8(const std::wstring& widestring)  
  96. {  
  97.     int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);  
  98.     if (utf8size == 0)  
  99.     {  
  100.         throw std::exception("Error in conversion.");  
  101.     }  
  102.   
  103.     std::vector<char> resultstring(utf8size);  
  104.   
  105.     int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);  
  106.   
  107.     if (convresult != utf8size)  
  108.     {  
  109.         throw std::exception("La falla!");  
  110.     }  
  111.   
  112.     return std::string(&resultstring[0]);  
  113. }  
  114.   
  115. //ascii 转 Utf8  
  116.   
  117. string ASCII2UTF_8(string& strAsciiCode)  
  118. {  
  119.     string strRet("");  
  120.   
  121.   
  122.     //先把 ascii 转为 unicode   
  123.     wstring wstr = Acsi2WideByte(strAsciiCode);  
  124.   
  125.     //最后把 unicode 转为 utf8  
  126.   
  127.     strRet = Unicode2Utf8(wstr);  
  128.   
  129.   
  130.     return strRet;  
  131. }  

你可能感兴趣的:(C/C++)