您的位置:首页 > 数据库

sqlite3数据库连接失败的问题

2017-05-15 10:01 267 查看
TCHAR  moduleFileName[MAX_PATH];  
::GetModuleFileName(NULL,moduleFileName, MAX_PATH);
(_tcsrchr(moduleFileName, _T('\\')))[1] = 0;
CString strPath = moduleFileName;
strPath.Append(_T("\database"));         //若把db文件拷贝至相对路径下(database文件夹与exe在一个目录下),出现数据库连接失败的问题。这是为什么?

//CString strPath="C:\\ccc\\database";   以此种方式连接,数据库连接成功。

if(_wmkdir(LPCWSTR(strPath.GetString())))
{
strPath.Append(_T("\\idea.db"));
std::string strDbName = CT2A(strPath);
m_db.SetDBName(strDbName.c_str());
if(!m_db.Connect())
{
MessageBox(NULL,_T("连接数据库失败"), "Error", MB_OK);
}

。。。

}

问题原因:

sqlite3中只支持UTF-8,所以需要将strDbName改为UTF-8格式的string。

将std::string strDbName=CT2A(strPath)改为wstring wstrPath = CT2W(strPath);
std::string strDbName = Unicode2Utf8(wstrPath);

则数据库连接成功。

还需要进一步了解多字节、Unicode、UTF-8各种编码方式之间的转换。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: