Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlDatabase problem. Is ever valid
Qt 6.11 is out! See what's new in the release blog

QSqlDatabase problem. Is ever valid

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xeyos
    wrote on last edited by
    #1

    Hi,
    I have a problem in using QSqlDatabase.
    If all the parameters are correct the connection is fine, but if I put a host that does not exist:
    db.open () returns true;
    db.isValid () returns true;
    How can I check then the host?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by mcosta
      #2

      Hi and welcome to devnet,

      could you post an example of that problem??

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • X Offline
        X Offline
        Xeyos
        wrote on last edited by
        #3

        My connection:
        db = QSqlDatabase::addDatabase(driver);
        db.setHostName(ui->_input_host->text());
        db.setUserName(ui->_input_user->text());
        db.setPassword(ui->_input_password->text());
        db.setPort(ui->_input_port->text().toInt());

        By inserting some hosts unavailable db.open () return true.
        I would first check if the host exists.
        you can also return the error on the user that connects?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

          what the driver are you using??

          is your code like this??

          void MyClass::connectToDb() {
              QSqlDatabase db = QSqlDatabase::addDatabase(driver);
              db.setHostName(ui->_input_host->text());
              db.setUserName(ui->_input_user->text());
              db.setPassword(ui->_input_password->text());
              db.setPort(ui->_input_port->text().toInt());
          
              if (db.open()) {
              // open
              } else {
              // error
              }
          }
          

          or QSqlDatabase::open() is called in different way?

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • X Offline
            X Offline
            Xeyos
            wrote on last edited by
            #5

            The user can choose the driver:
            In my case with QPSQL everything works properly and if you choose such QSQLITE connection occurs even though the driver is wrong.

            It is like the code you wrote:
            bool ok = db.open;
            if (ok) {
            ...
            } else {
            ...
            }

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by mcosta
              #6

              Hi,

              QSQLITE uses only databaseName (a sqlite database is a file in the current file system or is an in memory database if the name is :memory:).

              Read here to find the differences between drivers and how to set connenction parameters

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved