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"; 以此种方式连接,数据库连接成功。
{
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各种编码方式之间的转换。