前几天给程序加上了SQLite3数据库的支持,可是发现当把数据库文件放到有中文的目录下时就出问题了!
我前一阵写过 SQLite我选择我喜欢中介绍过,默认的接口的编码方式是UTF8,所以问题就在这里!
下面是我写的code,该文章版权归本人所有,代码大家可以随意使用!
char szFileNameUtf8[MAX_PATH*4];
_bstr_t bstrFileName = strPath;
if(HasChineseChar(strPath))
{
AtlUnicodeToUTF8(bstrFileName,bstrFileName.length(),szFileNameUtf8,sizeof(szFileNameUtf8));
result = sqlite3_open(szFileNameUtf8, &m_sqlite3DB);
}
else
{
result = sqlite3_open(strPath, &m_sqlite3DB);
}
if (result == SQLITE_OK)
{
char *errmsg = NULL;
int nError = sqlite3_exec( m_sqlite3DB, "create table Table_a( ID integer primary key autoincrement, name nvarchar(32) )", NULL, NULL, &errmsg );
if(nError != SQLITE_OK)
{
//这里不需要log.
ATLTRACE( "创建表失败,错误码:%d,错误原因:%s/n", result, errmsg );
}
//else cont.
}
如果你发现有什么问题请给我留言让我知道!
HasChineseChar这个api是我上篇文章中提供的代码:http://blog.youkuaiyun.com/wangweixing2000/archive/2007/09/04/1771351.aspx
转载需经过本人同意!
我前一阵写过 SQLite我选择我喜欢中介绍过,默认的接口的编码方式是UTF8,所以问题就在这里!
下面是我写的code,该文章版权归本人所有,代码大家可以随意使用!
char szFileNameUtf8[MAX_PATH*4];
_bstr_t bstrFileName = strPath;
if(HasChineseChar(strPath))
{
AtlUnicodeToUTF8(bstrFileName,bstrFileName.length(),szFileNameUtf8,sizeof(szFileNameUtf8));
result = sqlite3_open(szFileNameUtf8, &m_sqlite3DB);
}
else
{
result = sqlite3_open(strPath, &m_sqlite3DB);
}
if (result == SQLITE_OK)
{
char *errmsg = NULL;
int nError = sqlite3_exec( m_sqlite3DB, "create table Table_a( ID integer primary key autoincrement, name nvarchar(32) )", NULL, NULL, &errmsg );
if(nError != SQLITE_OK)
{
//这里不需要log.
ATLTRACE( "创建表失败,错误码:%d,错误原因:%s/n", result, errmsg );
}
//else cont.
}
如果你发现有什么问题请给我留言让我知道!
HasChineseChar这个api是我上篇文章中提供的代码:http://blog.youkuaiyun.com/wangweixing2000/archive/2007/09/04/1771351.aspx
转载需经过本人同意!

本文分享了解决SQLite3在中文路径下无法正常工作的问题。通过判断路径是否包含中文,并将中文路径转换为UTF8编码,成功实现了在含有中文的目录下使用SQLite3数据库。
2962

被折叠的 条评论
为什么被折叠?



