SQLDataBase setDatabaseName 在 QT 中不起作用
SQLDataBase setDatabaseName doesn't work in QT
我在一个包含这些代码的类中有两个方法,在方法GetDefinitionOfWord中,首先我调用了GetDictionaryFilePath正确返回DB的名称,但在方法GetDefinitionOfWord中,当执行DB . setdatabasename (GetDictionaryFilePath(ID));
它没有设置数据库名称,不能打开数据库,我会得到错误,我怎么能解决这个问题?
请帮帮我
QString Dictionary_Operation::GetDefinitionOfWord(QString ID, QString Word)
{
QString Result = "";
QString FinalResult = "";
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QString DBOpenErrorTitle = QString::fromStdString("Error");
QString DBOpenErrorMessage = QString::fromStdString("Access denied.");
QString FileName = GetDictionaryFilePath(ID);
db.setDatabaseName(GetDictionaryFilePath(ID));
if (QFile::exists(QString::fromStdString(".\" + FileName.toStdString()))) {
db.setDatabaseName(GetDictionaryFilePath(ID));
if (!db.open()) {
QMessageBox::critical(0, DBOpenErrorTitle, DBOpenErrorMessage,
QMessageBox::Cancel);
}
else
{
QSqlQuery query;
query.exec(QString::fromStdString("PRAGMA encoding = UTF-16"));
QString s = QString::fromStdString("SELECT Definition FROM Dictionary_Words WHERE HeadWord = '%1'").arg(ID);
QSqlQuery sql(s, db);
while ( sql.next() )
{
Result = Result.append(sql.record().value(0).toString());
}
db.close();
FinalResult = ReplaceImageToBase64(Result, ID);
}
}
QSqlDatabase::removeDatabase(FileName);
return FinalResult;
}
另一种方法是:
QString Dictionary_Operation::GetDictionaryFilePath(QString ID)
{
QString Result = "0";
QSqlDatabase dbGetDictionaryFilePath = QSqlDatabase::addDatabase("QSQLITE");
QString DBOpenErrorTitle = QString::fromStdString("Error");
QString DBOpenErrorMessage = QString::fromStdString("Access denied.");
if (QFile::exists(".\1.pldb")) {
dbGetDictionaryFilePath.setDatabaseName(QString::fromStdString("1.pldb"));
if (!dbGetDictionaryFilePath.open()) {
QMessageBox::critical(0, DBOpenErrorTitle, DBOpenErrorMessage,
QMessageBox::Cancel);
}
else
{
QSqlQuery query;
query.exec(QString::fromStdString("PRAGMA encoding = UTF-16"));
QString s = QString::fromStdString("SELECT FileName FROM Dictionaries WHERE ID = %1").arg(ID);
QSqlQuery sql(s, dbGetDictionaryFilePath);
while ( sql.next() )
{
Result = sql.record().value(0).toString();
}
// dbGetDictionaryFilePath.close();
}
}
QSqlDatabase::removeDatabase(QString::fromStdString("1.pldb"));
return Result;
}
您使用了两次相同的连接,并且配置部分重叠。因此,当您调用setDatabaseName
时,您是在GetDictionaryFilePath
函数已经打开的连接上调用它。
你应该使用2个不同的连接名:
QString Dictionary_Operation::GetDefinitionOfWord(QString ID, QString Word)
{
...
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "Definitions");
QString FileName = GetDictionaryFilePath(ID);
db.setDatabaseName(FileName);
...
// Remove with the name of the connection (and not databaseName())
QSqlDatabase::removeDatabase("Definitions");
...
}
QString Dictionary_Operation::GetDictionaryFilePath(QString ID)
{
QSqlDatabase dbGetDictionaryFilePath =
QSqlDatabase::addDatabase("QSQLITE", "Dictionaries");
...
dbGetDictionaryFilePath.setDatabaseName("1.pldb");
...
QSqlDatabase::removeDatabase("Dictionaries");
}
或者你可以使用一个连接名称为每个不同的"定义"数据库通过传递FileName
而不是字符串"Definitions"
到addDatabase
和removeDatabase
在你的第一个函数。
PS:为什么你使用QString::fromStdString ?您可以直接将文字字符串传递给期望const QString &
的函数(您已经为addDatabase("QSQLITE")
做过了)。
相关文章:
- 按钮悬停在 QT 中垂直布局的选项卡小部件中不起作用
- Qt信号和插槽如果从QRunnable或其他线程调用,则不起作用
- 在qt QFileDialog中设置后缀在linux中不起作用,如何解决?
- 当我的阵列达到一定大小时,Qt 不起作用
- Qt creator语言 - fno-elide-constructors 不起作用
- 谷歌语音识别不起作用,因为冲突线程Qt C++
- QT QML:使用.qrc文件将SQLite数据库部署到Android不起作用
- (typedef)typename迭代器在c++、qt中不起作用
- qt.bluetooth:虚拟后端正在运行.Qt蓝牙模块在Raspbian上不起作用
- QT创建者 - selecteddate()不起作用
- QT在CentOS 7的openGL不起作用
- QT 资源不起作用
- 与 alpha 通道的图像重叠在 QT 上不起作用
- Qt QProcess有时有效,有时不起作用
- 我的代码在Qt Creator中工作,但在Cloud9中不起作用
- 我的代码在发布模式下不起作用,而在Qt的调试模式下工作
- Qt QFrame StyleSheet 在 MainWindow 中不起作用
- QT HTML 与对齐不起作用
- QT连接到按钮不起作用
- QNetworkReply https API 在 Android 设备上不起作用 - QT