Qt使用SQL的问题

绝世神棍 2013-12-18 11:01:42
我刚开始学Qt,工程里按教程使用了Sql,代码如下:

bool MainWindow::initDatabase()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setDatabaseName("loader");
db.setUserName("root");
db.setPassword("root");
if(!db.open())
{
QMessageBox::critical(this,QString::fromStdWString(L"连接数据库失败"), db.lastError().text());
return false;
}
return true;
}


pro文件里也添加了 QT += sql
但编译的时候提示找不到定义,但我的pro文件里不是添加了sql模块吗?开发环境是Qt5.2+VS2010+WINXP
各位大哥有知道的吗?谢谢了


mainwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase@@QAE@XZ),该符号在函数 "private: bool __thiscall MainWindow::initDatabase(void)" (?initDatabase@MainWindow@@AAE_NXZ) 中被引用
mainwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __thiscall QSqlError::~QSqlError(void)" (__imp_??1QSqlError@@QAE@XZ),该符号在函数 "private: bool __thiscall MainWindow::initDatabase(void)" (?initDatabase@MainWindow@@AAE_NXZ) 中被引用
mainwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: class QString __thiscall QSqlError::text(void)const " (__imp_?text@QSqlError@@QBE?AVQString@@XZ),该符号在函数 "private: bool __thiscall MainWindow::initDatabase(void)" (?initDatabase@MainWindow@@AAE_NXZ) 中被引用
mainwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: class QSqlError __thiscall QSqlDatabase::lastError(void)const " (__imp_?lastError@QSqlDatabase@@QBE?AVQSqlError@@XZ),该符号在函数 "private: bool __thiscall MainWindow::initDatabase(void)" (?initDatabase@MainWindow@@AAE_NXZ) 中被引用
mainwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: bool __thiscall QSqlDatabase::open(void)" (__imp_?open@QSqlDatabase@@QAE_NXZ),该符号在函数 "private: bool __thiscall MainWindow::initDatabase(void)" (?initDatabase@MainWindow@@AAE_NXZ) 中被引用
mainwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __thiscall QSqlDatabase::setPassword(class QString const &)" (__imp_?setPassword@QSqlDatabase@@QAEXABVQString@@@Z),该符号在函数 "private: bool __thiscall MainWindow::initDatabase(void)" (?initDatabase@MainWindow@@AAE_NXZ) 中被引用
mainwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __thiscall QSqlDatabase::setUserName(class QString const &)" (__imp_?setUserName@QSqlDatabase@@QAEXABVQString@@@Z),该符号在函数 "private: bool __thiscall MainWindow::initDatabase(void)" (?initDatabase@MainWindow@@AAE_NXZ) 中被引用
mainwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __thiscall QSqlDatabase::setDatabaseName(class QString const &)" (__imp_?setDatabaseName@QSqlDatabase@@QAEXABVQString@@@Z),该符号在函数 "private: bool __thiscall MainWindow::initDatabase(void)" (?initDatabase@MainWindow@@AAE_NXZ) 中被引用
mainwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __thiscall QSqlDatabase::setHostName(class QString const &)" (__imp_?setHostName@QSqlDatabase@@QAEXABVQString@@@Z),该符号在函数 "private: bool __thiscall MainWindow::initDatabase(void)" (?initDatabase@MainWindow@@AAE_NXZ) 中被引用
mainwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: static class QSqlDatabase __cdecl QSqlDatabase::addDatabase(class QString const &,class QString const &)" (__imp_?addDatabase@QSqlDatabase@@SA?AV1@ABVQString@@0@Z),该符号在函数 "private: bool __thiscall MainWindow::initDatabase(void)" (?initDatabase@MainWindow@@AAE_NXZ) 中被引用
mainwindow.obj : error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: static char * QSqlDatabase::defaultConnection" (__imp_?defaultConnection@QSqlDatabase@@2PADA)
debug\Meter.exe : fatal error LNK1120: 11 个无法解析的外部命令
...全文
373 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
绝世神棍 2013-12-23
  • 打赏
  • 举报
回复
结贴,问题解决了,要把项目下面的DEBUG文件夹全部删除,这样才能重建某些源文件的OBJ。用QtCreator里的清除功能没用。谢谢各位了。
mrx102 2013-12-18
  • 打赏
  • 举报
回复
用VS编译的时候好像跟pro文件没有关系了吧,你看看VS里面有没有添加Qt5Sql.lib;
绝世神棍 2013-12-18
  • 打赏
  • 举报
回复
引用 1 楼 Inhibitory 的回复:
安装了MySql的驱动了没有?
安了,本机上安装了MYSQL和MYSQL的ODBC驱动。还照着网上教程在Qt里编译了MYSQL驱动,但文件夹下5.2.0\msvc2010\plugins\sqldrivers里早就有相应的DLL了。这错误应该是没有链接到Qt5Sql,不知道为啥pro设置了却没起作用
Inhibitory 2013-12-18
  • 打赏
  • 举报
回复
安装了MySql的驱动了没有?

16,211

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧