У меня есть два метода в одном классе, которые содержат этот код, в методе GetDefinitionOfWord, сначала я вызывал GetDictionaryFilePath, который правильно возвращает имя БД, но в методе 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;
}