解决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<wchar_t> 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<char> 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<wchar_t> 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<char> 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;
} 

时间: 2024-08-02 01:03:37

解决SQLite数据库中文乱码问题的相关文章

java.sql.Connection解决插入数据库中文乱码问题

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectionManager { private static final String DRIVER = "com.mysql.jdbc.Driver"; //private static final String URL = "jdbc:mysql://localh

Net、c# 连接Mysql数据库中文乱码

网上有两种解决方案: 第一种是,每次执行语句的时候都和PHP的类似,先执行 一句“set names utf8”或者“set names gb2312”; 1 MySQLCommand setformat = new MySQLCommand("set names b2312",m_Connection); 2 setformat.ExecuteNonQuery(); 3 setformat.Dispose(); 第二种是,在webconfig里加一句“Charset=gbk”; 1

python读取数据库中文乱码问题

今天朋友遇到过怪问题,在同一个页面显示的2条中文记录一个正常,一个乱码,2条记录分别从不同的表里取出.录入的时候采用直接录入.仔细观察2者区别,发现能正常显示的字段在表中类型为nvarchar,不能的是varchar,试着将其改成nvarchar,问题解决. 因为对sqlserver不熟悉.通过查询手册得知: nvarchar表示以Unicode格式存储可变长度的 数据,所以能显示中文,而varchar是用非unicode存储数据,所以乱码.将Varchar类型设置为nvarchar类型,发现问

教你解决Sublime Text中文乱码问题

前面一篇开始学习solr的时候,做了个入门的示例http://blog.csdn.net/zjc/article/details/24414271 .虽然可以检索出内容,但总和想象的结果有差异--比如,检索"天龙"两个字,按常规理解,就应该只出来<天龙八部>才对,可是竟然也会把<倚天屠龙记>检出来.后来研究了一下,发现系统是这样处理的:无论是抽索引时还是分析检索词时,都把所有文字按单字拆开.这样,刚好<倚天屠龙记>里包含"天"和&

使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)

前述: 在写这篇笔记之前,对笔记中的设计模式进行介绍: 本篇笔记中将要使用到的设计模式是:装饰(包装)设计模式 (1)装饰(包装)设计模式口诀: ①定义一个类,实现被装饰对象的接口 ②定义一个成员变量,记住被装饰对象的引用 ③定义构造方法,传入被装饰对象的实例 ④改写要修改的方法 ⑤不需要改写的方法,调用被装饰对象的原来的方法 (2)什么时候使用装饰设计模式 当我们需要对一个类进行增强的时候,增强后的类不再当前类的范畴 例如:现在有一个     Animal类     Cat和Dog都属于动物类

解决Sublime Text2 中文乱码

1.安装Sublime Package Control 在Sublime Text 2上用Ctrl+-打开控制台并在里面输入以下代码,Sublime Text 2就会自动安装Package Control. import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else N

(转)Delphi 调用C#编写的WebService 参数为Null解决方法 附中文乱码问题

(转)Delphi 调用C#编写的WebService 参数为Null解决方法 附中文乱码问题 //add-------to support UTF-8     RIO.HTTPWebNode.UseUTF8InHeader := true;  //添加该行,指定采用UTF-8代码传输     RIO.Converter.Encoding:='UTF-8';     RIO.Converter.Options:=RIO.Converter.Options + [soUTF8InHeader,so

解决Ubuntu系统中文乱码显示问题,终端打开文件及查看目录

解决Ubuntu系统中文乱码显示问题 [日期:2014-02-20] 来源:Linux社区  作者:njchenyi [字体:大 中 小] 我是先安装了Ubuntu 12.04 Server,然后安装桌面.进入以后发现中文有问题. 解决方法:一. Ubuntu默认的中文字符编码Ubuntu默认的中文字符编码为zh_CN.UTF-8,这个可以在/etc/environment中看到:sudo gedit /etc/environment可以看到如下内容:PATH="/usr/local/sbin:

JAVA压缩 解压缩zip 并解决linux下中文乱码

1. [代码][Java]代码   1:再压缩前,要设置linux模式, 需要使用第三方ant-1.6.5.jar  如果是文件目录,则ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty("file.separator"));zipEntry.setUnixMode(755);//解决linux乱码 如果是文件,则 ZipEntry zipEntry=new ZipEntry(base