SQLDataBase setDatabaseName 在 QT 中不起作用 [英] SQLDataBase setDatabaseName doesn't work in QT
问题描述
我在一个包含这些代码的类中有两个方法,在方法 GetDefinitionOfWord 中,起初我一直在调用 GetDictionaryFilePath 来正确返回 DB 的名称,但在执行 db.setDatabaseName(GetDictionaryFilePath(ID)) 时在方法 GetDefinitionOfWord 中;
I have two method in one class that contain these code, In method GetDefinitionOfWord, at first i've been call GetDictionaryFilePath that correctly return the name of DB, but in method GetDefinitionOfWord when execute db.setDatabaseName(GetDictionaryFilePath(ID));
它没有设置数据库名称并且无法打开数据库,我会收到错误消息,我该如何解决?
It doesn't set the database name and can't open DB and i'll get error, how can i fix this?
请帮帮我
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
函数在已经打开的连接上调用它.
You are using the same connection twice and the configuration parts are overlapping. So, when you call setDatabaseName
, you are calling it on the already open connection from your GetDictionaryFilePath
function.
您应该使用 2 个不同的连接名称:
You should use 2 different connection names:
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
在你的第一个函数中.
Or you can use one connection name for each different "definition" database by passing FileName
instead of the string "Definitions"
to both addDatabase
and removeDatabase
in your first function.
PS:你为什么使用 QString::fromStdString ?您可以将文字字符串直接传递给需要 const QString &
的函数(并且您已经为 addDatabase("QSQLITE")
完成了它).
PS: why do you use QString::fromStdString ? You can pass directly literal string to functions that expect a const QString &
(and you already done it for addDatabase("QSQLITE")
).
这篇关于SQLDataBase setDatabaseName 在 QT 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!