vc6 sqlite3 插入中文数据问题
下面代码插入英文数据无任何问题,可如果插入中文数据就有问题,要怎么才能把语句转成utf-8,新手望赐教#include<stdio.h>#include<stdlib.h...
下面代码插入英文数据无任何问题,可如果插入中文数据就有问题,要怎么才能把语句转成utf-8,新手望赐教
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int mt()
{
sqlite3 *db=NULL;
char *errMsg = NULL;
int rc;
char *sql = NULL;
rc = sqlite3_open("test.db", &db);
if( rc ){
exit(1);
}
rc = sqlite3_exec(db, "INSERT INTO person_list VALUES('test','','','','0','-1');", NULL, NULL, &errMsg); //无问题
rc = sqlite3_exec(db, "INSERT INTO person_list VALUES('测试','','','','0','-1');", NULL, NULL, &errMsg); //有问题
sqlite3_close(db);
return 0;
} 展开
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int mt()
{
sqlite3 *db=NULL;
char *errMsg = NULL;
int rc;
char *sql = NULL;
rc = sqlite3_open("test.db", &db);
if( rc ){
exit(1);
}
rc = sqlite3_exec(db, "INSERT INTO person_list VALUES('test','','','','0','-1');", NULL, NULL, &errMsg); //无问题
rc = sqlite3_exec(db, "INSERT INTO person_list VALUES('测试','','','','0','-1');", NULL, NULL, &errMsg); //有问题
sqlite3_close(db);
return 0;
} 展开
2个回答
展开全部
#include <windows.h>
static WCHAR *mbcsToUnicode(const char *zFilename){
int nByte;
WCHAR *zMbcsFilename;
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
zMbcsFilename = (WCHAR*)malloc( nByte*sizeof(zMbcsFilename[0]) );
if( zMbcsFilename==0 ){
return 0;
}
nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
if( nByte==0 ){
free(zMbcsFilename);
zMbcsFilename = 0;
}
return zMbcsFilename;
}
static char *unicodeToUtf8(const WCHAR *zWideFilename){
int nByte;
char *zFilename;
nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
zFilename = (char*)malloc( nByte );
if( zFilename==0 ){
return 0;
}
nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
0, 0);
if( nByte == 0 ){
free(zFilename);
zFilename = 0;
}
return zFilename;
}
static WCHAR *utf8ToUnicode(const char *zFilename)
{
int nChar;
WCHAR *zWideFilename;
nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
zWideFilename = (WCHAR*) malloc( nChar*sizeof(zWideFilename[0]) );
if( zWideFilename==0 ){
return 0;
}
nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
if( nChar==0 ){
free(zWideFilename);
zWideFilename = 0;
}
return zWideFilename;
}
static char *unicodeToMbcs(const WCHAR *zWideFilename)
{
int nByte;
char *zFilename;
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
zFilename = (char*)malloc( nByte );
if( zFilename==0 ){
return 0;
}
nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
0, 0);
if( nByte == 0 ){
free(zFilename);
zFilename = 0;
}
return zFilename;
}
char *AnsiToUtf8( const char *zFilename) /** ansi 2 utf8 */
{
char *zFilenameUtf8;
WCHAR *zTmpWide;
zTmpWide = mbcsToUnicode(zFilename);
if( zTmpWide==0 ){
return 0;
}
zFilenameUtf8 = unicodeToUtf8(zTmpWide);
free(zTmpWide);
return zFilenameUtf8;
}
char *Utf8ToAnsi( char *zFilename) /*** utf8 - ansi */
{
char *zFilenameMbcs;
WCHAR *zTmpWide;
zTmpWide = utf8ToUnicode(zFilename);
if( zTmpWide==0 ){
return 0;
}
zFilenameMbcs = unicodeToMbcs(zTmpWide);
free(zTmpWide);
return zFilenameMbcs;
}
其实插入sqlite3数据库是ansi格式数据再取出来还是ansi, 显示应该是没有问题的.
找个支持ansi的sqlite3客户端就行了. 比如 sqlit3expert
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询