利用BCB6.0 往SQLite中写入中文出现乱码,高分找高手解决
源码:
sSQL = "insert into t_escxstyfpb(C_FPXH,C_MFDWDMSFZHM1,C_MFDWGRZZ1,"
"C_MFDH1,C_MFDWGR2,C_MFDWDMSFZHM2,C_MFDWGRZZ2,C_MFDH2,C_CPZH,"
"C_DJZH,C_CLLX,C_CJHCLSBDM,C_CPXH,C_ZRDCLGLSMC,C_JYPMDW,"
"C_JYPMDWDZ,C_JYPMDWNSRSBH,C_JYPMDWKHYH,C_JYPMDWDH,C_ESCSC,"
"C_ESCSCNSRSBH,C_ESCSCDZ,C_ESCSCKHYHZH ,C_ESCSCDH) values ("
":C_FPXH,:C_MFDWDMSFZHM1,:C_MFDWGRZZ1,:C_MFDH1,:C_MFDWGR2,"
":C_MFDWDMSFZHM2,:C_MFDWGRZZ2,:C_MFDH2,:C_CPZH,:C_DJZH,:C_CLLX,"
":C_CJHCLSBDM,:C_CPXH,:C_ZRDCLGLSMC,:C_JYPMDW,:C_JYPMDWDZ,"
":C_JYPMDWNSRSBH,:C_JYPMDWKHYH,:C_JYPMDWDH,:C_ESCSC,"
":C_ESCSCNSRSBH,:C_ESCSCDZ,:C_ESCSCKHYHZH,:C_ESCSCDH)";
qry->Parameters->ParamByName("C_MFDWDMSFZHM1")->Value = AnsiToUtf8(edtMFDWDMSFZHM1->Text);
qry->Parameters->ParamByName("C_MFDWGRZZ1")->Value = AnsiToUtf8(edtMFDWGRZZ1->Text);
qry->Parameters->ParamByName("C_MFDH1")->Value = AnsiToUtf8(edtMFDH1->Text);
qry->Parameters->ParamByName("C_MFDWGR2")->Value = AnsiToUtf(edtMFDWGR2->Text);
qry->Parameters->ParamByName("C_MFDWDMSFZHM2")->Value = AnsiToUtf8(edtMFDWDMSFZHM2->Text);
......
问题:界面上输入汉字时如果是单个汉字出现乱码,如果是非汉字与汉字一起时出现乱码,其他情况正常。。
解决方法一:
有一种方式用直接给这SQL语句赋值操作,但本工程大,那样改装的话需要改许多地方。
所以,寻找另一种可行之法,能解决这个问题的,又可以不要使我目前工程改动太大。找高手帮 忙。
[解决办法]
应该是SQLITE最低层的API调用错误所致。
它的API有两套,一套是for Unicode的,就不会乱码。
你用的是什么控件呢?
[解决办法]
用unicodeString吧
[解决办法]
字符编码的问题,统一用一种字符编码,SQLite中用Ansi就能正常显示中文了
[解决办法]
直接用它的DLL包吧,就不会有这种问题
[解决办法]
数据库用utf-8的话,用下面的函数先把数据转成utf8存库,读取的时候再转成gb2312显示在控件上
//UTF-8到GB2312的转换
char* U2G(const char* utf8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wStr = new wchar_t[len + 1];
memset(wStr, 0, len+1);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wStr, len);
len = WideCharToMultiByte(CP_ACP, 0, wStr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wStr, -1, str, len, NULL, NULL);
if (wStr) delete[] wStr;
return str;
}
//---------------------------------------
//GB2312到UTF-8的转换
char* G2U(const char* gb2312)
{
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
wchar_t* wStr = new wchar_t[len + 1];
memset(wStr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wStr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wStr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wStr, -1, str, len, NULL, NULL);
if (wStr) delete[] wStr;
return str;
}