QSqlDatabase problem. Is ever valid



  • 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?



  • Hi and welcome to devnet,

    could you post an example of that problem??



  • 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?



  • 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?



  • 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 {
    ...
    }



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.