开发者

SQLDataBase setDatabaseName doesn't work in QT

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?

Please help me

    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;
    }

and Other method is:

      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;
        }


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.

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"); 
}

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: 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")).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜