2011-09-11 63 views
2

我在一個類中有兩個方法包含這些代碼,在GetDefinitionOfWord方法中,起初我一直調用GetDictionaryFilePath,它正確地返回DB的名字,但是在方法GetDefinitionOfWord中當execute db .setDatabaseName(GetDictionaryFilePath(ID));SQLDataBase setDatabaseName在QT中不起作用

它沒有設置數據庫名稱,無法打開數據庫,我會得到錯誤,我該如何解決這個問題?

請幫我

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

回答

0

您使用的是相同的連接兩次,並配置部分重疊。因此,當您撥打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"的既addDatabaseremoveDatabase使用一個連接名稱爲每個不同的「定義」數據庫。

PS:爲什麼使用QString :: fromStdString?您可以直接將字符串傳遞給期望const QString &的函數(並且您已經完成了addDatabase("QSQLITE"))。