[英]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");
}
或者,可以通过在第一个函数addDatabase
FileName
而不是字符串"Definitions"
传递给addDatabase
和removeDatabase
来为每个不同的“定义”数据库使用一个连接名称。
PS:为什么使用QString :: fromStdString? 您可以将文字字符串直接传递给需要const QString &
的const QString &
(并且您已经对addDatabase("QSQLITE")
做到了)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.