Qt 5.4,数据库未打开错误
Qt 5.4, database not open error
我是C++/Qt的新手,正在尝试写入我在项目的根目录中创建的SQLite数据库。
以下是我一直使用的众多网站之一以供参考:http://www.java2s.com/Code/Cpp/Qt/ConnecttoSqliteanddoinsertdeleteupdateandselect.htm
我创建了一个函数,该函数接收所有用户输入的值作为对象的参数。我想将用户输入的这些值写入我的根目录数据库。
这是获取数据库对象并尝试将它们写入我的根级数据库"matrixics.db"的函数。
我的职能:
#include <QtSql>
#include <QSqlDatabase>
#include <QtDebug>
#include <QFile>
#include <QMessageBox>
void db_connection::writeDBValues(db_config *dbInfo)
{
//connect to matrixics.db
QSqlDatabase matrixics = QSqlDatabase::addDatabase("QSQLITE", "MatrixICS");
//using absolute path doesn't work either
//matrixics.setDatabaseName("D:/Qt Projects/build-MatrixICS-Desktop_Qt_5_4_0_MinGW_32bit-Debug/matrixics.db");
matrixics.setDatabaseName("./matrixics.db");
if(!QFile::exists("./matrixics.db"))
{
QString failedMsg = tr("FAILED: Could not locate matrixics.db file");
QMessageBox::warning(this, tr("ERROR: Failed MatrixICS Database Connection!"), failedMsg);
}
else if (!matrixics.open())
{
QString failedMsg = tr("FAILED: ") + matrixics.lastError().text();
QMessageBox::warning(this, tr("ERROR: Failed to open matrixics.db!"), failedMsg);
}
else
{
//write db values
QSqlQuery qry;
if (m_connectionName == "localDb")
{
qry.prepare( "INSERT INTO settings (local_db_type, local_db_host, local_db_port, local_db_user, local_db_pass) VALUES (?, ?, ?, ?, ?)" );
}
else if (m_connectionName == "remoteDb")
{
qry.prepare( "INSERT INTO settings (remote_db_type, remote_db_host, remote_db_port, remote_db_user, remote_db_pass) VALUES (?, ?, ?, ?, ?)" );
}
//bind all values
qry.addBindValue(dbInfo->m_db_type);
qry.addBindValue(dbInfo->m_hostname);
qry.addBindValue(dbInfo->m_port);
qry.addBindValue(dbInfo->m_db_user);
//encode user pass
//base64_encode is included in a globals.h fn not shown above
QString encodedPass = base64_encode(dbInfo->m_db_pass);
qry.addBindValue(encodedPass);
if(!qry.exec()) qDebug() << qry.lastError();
matrixics.close();
}
QSqlDatabase::removeDatabase("MatrixICS");
}
错误:
QSqlQuery::prepare: database not open
QSqlError("", "Driver not loaded", "Driver not loaded")
QSqlDatabasePrivate::removeDatabase: connection 'MatrixICS' is still in use, all queries will cease to work.
我的问题:
脚本如何在不打开数据库的情况下将其简化为QSqlQuery::prepare
函数?我认为当我执行if (!matrixics.open())
时,如果数据库未打开,脚本会抛出我的"无法打开"消息。但是,事实并非如此,因此逻辑应该规定数据库实际上是开放的,但我收到QSqlQuery::prepare: database not open
.
您的QSqlQuery
对象必须使用对QSqlDatabase
的引用来构造。换句话说,使这条线
QSqlQuery qry(matrixics);
请参阅QSqlQuery::QSqlQuery
文档。
相关文章:
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 以只读模式打开数据库时SQLITE_CANTOPEN错误
- 【MacOSX 10.15.1】使用 odb 生成数据库,致命错误:wchar.h:没有这样的文件或目录 #include < wchar.h>
- QT 数据库模块错误
- 如何修复数据库连接的C 程序运行时间错误(0xc0000005)
- Qt & SqlLite - 错误的数据库路径
- 在CodeBlocks上编译C 中的Oracle数据库环境时的错误
- 数据库未打开错误QT C
- CDao数据库断言错误
- ODBC 错误"String data, right truncation State code: 22001"与 SQL Server 数据库
- 几个使用相同原型的共享对象导致错误:数据库中已经存在文件
- Qt 5.4,数据库未打开错误
- 使用C++从MySQL数据库获取字符串时编码错误
- 为什么我得到错误C1033:无法打开VS 2010中的程序数据库
- 使用SOCI从PostgreSQL数据库获取数据时发生错误转换
- SQLite删除数据库错误
- Sybase DB 错误:数据库驱动程序错误.发送失败
- 在 sqlite 数据库 qt 之间复制表会导致错误
- 将数据从qt插入postgres数据库错误