【发布时间】:2017-03-25 08:21:37
【问题描述】:
我实际上正在使用 Qt 和 SqlLite,我的问题是,即使是完全在 sqllite3.exe 上运行的请求也无法在我的程序中运行。我不断收到我设置的错误消息。我想知道这是否真的是代码问题,因为我实际上可以连接到我的数据库,并且在尝试将它们放入 sqllite3.exe 时请求工作正常。
数据库路径:
SQLLITE\musclelayout
DataHandler.cpp - 构造函数(经过许多有用的 cmets 编辑)
DataHandler::DataHandler(QMainWindow* mw, const QString& path)
{
databaseIsOpen = false;
database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName(path);
qDebug() << "Opening database:" << path << "from" << QDir::currentPath();
qDebug() << "Does the file exist:" << QFileInfo::exists(path);
if(database.open())
{
qDebug() << "Database opened. Tables:" << database.tables();
databaseIsOpen = true;
}
else
{
QString msgTitle("Cannot login to '" + path + "'");
QMessageBox::StandardButton alertMsg;
alertMsg = QMessageBox::warning(mw, msgTitle, database.lastError().text(), QMessageBox::Ok);
}
}
路径完全错误:
Opening database: "SQLLITEmusclelayout" from "C:/Users/Tong3/Desktop/DEV/build-muscleLayout-Qt_5_8_0_mingw53_32-Debug"
Does the file exist: true
Database opened. Tables: ()
但我无法编写'SQLLITE\'(不会编译),并且路径没有转到我的项目所在的位置。 真实网址:
C:\Users\Tong3\Desktop\DEV\muscleLayout\SQLLITE\musclelayout
DataHandler - getTrainedMucleList()
vector<QString> DataHandler::getTrainedMuscleList(QMainWindow* mw)
{
vector<QString> tmp_array;
QSqlQuery query;
if(databaseIsOpen)
{
if(query.exec("SELECT * FROM trainedMuscle"))
{
while(query.next())
{
tmp_array.push_back(query.value("muscleName").toString());
}
}
else
{
QString msgTitle("Cannot load muscle list");
QMessageBox::StandardButton alertMsg;
alertMsg = QMessageBox::warning(mw, msgTitle, query.lastError().text(), QMessageBox::Ok);
}
}
else
{
QString msgTitle("Cannot login to database");
QMessageBox::StandardButton alertMsg;
alertMsg = QMessageBox::warning(mw, msgTitle, database.lastError().text(), QMessageBox::Ok);
}
return tmp_array;
}
感谢您花时间阅读我的新手代码, 祝你有美好的一天。
【问题讨论】:
-
使用
query.lastError().text();查看错误信息。 -
路径是什么?
-
错误是“找不到受过训练的肌肉”。但表确实存在!路径是“SQLLITE\musclelayout”。数据库打开成功