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);
则数据库连接成功。
::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各种编码方式之间的转换。
相关文章推荐
- ASP.NET2.0网站配置的数据库连接失败问题 (转载)
- c++中sqlite中文路径创建数据库失败的问题
- 解决php原生连接数据库失败的问题(1)
- Metasploit 数据库连接失败的问题
- Windows7旗舰版安装PLSQLDeveloper连接数据库遇到:ora-12514 问题和ORA-12545:因目标主机或对象不存在,连接失败”解决方法
- SQL server 2014安装以及解决连接数据库失败问题
- 项目报错:打开文件过多,数据库连接失败等资源问题
- sqlite打开路径含中文的数据库文件失败的问题
- 解决cognos创建数据源连接本地数据库失败问题
- Java程序数据库连接失败问题
- SQL Server连接数据库失败,可能的问题!
- 使用 EF Core 的 EnableRetryOnFailure 解决短暂的数据库连接失败问题
- ASP.NET2.0网站配置的数据库连接失败问题(没有安装SQLEXPRESS数据库的时候)
- vc 连接数据库CreateInstance失败问题
- 如何修复VUM在客户端启用之后报数据库连接失败的问题
- informatica 9.6.1 安装过程中数据库连接失败,通过查看安装日志对症解决问题
- 解决php连接数据库失败却无报错问题
- Typecho SQLite与MySQL的数据库切换及解决MySQL连接打开缓慢问题
- 如何修复VUM在客户端启用之后报数据库连接失败的问题
- 如何处理数据库连接空闲太久导致失败的问题