Database result with connection name in all project



  • Hello, my problem of day is: how use in all class of my project one ore more database? I have try with setting connection name.

    In my project I have a class with name "DataBase" that opens a database giving a name to the connection like that:

    db = QSqlDatabase::addDatabase(driver_db, "pippo");
    

    In the same project in several parts I have to use the same database. I thought about connecting to the database by calling the connection with its name

    DataBase *database = new DataBase;
    QSqlDatabase dbstructure = QSqlDatabase::database("pippo");
    Although the connection is open and I do not have any error reports when I try to read a value of a selected record I do not return anything to the query.size () indicates me -1
            if(database->openDataBase(dbstructure)) {
            QSqlQuery querydb(dbstructure);
            QString strqrysearchprogram = "select * from nome_Tabella where Item  = '1'" ;
            if(querydb.exec(strqrysearchprogram)) {
                if (querygrare.first()) {
                    qDebug() << querygrare.size();
                    QString valuefield = querygrare.value("nameField").toString();
                    qDebug() << "valuefield : " << valuefield;
                    qDebug() << "qry: " << strqrysearchprogram;
                }
            } else {
                qDebug() << "record non trovato in qry: " << strqrysearchprogram;
            }
        } else {
            qDebug() << "ERROR open Database";
        }
    
    

    Where am I wrong? Is it correct to recall the database with the name of the connection? I state that I have checked the selection string that is passed and the record exists.


  • Lifetime Qt Champion

    Can you show your complete code ?



  • Yes, this is method for connection:

     QSqlDatabase DataBase::connectToDataBase(int type_db, QString path_and_name_db, QString driver_db, QString user_db, QString pass_db, QString hostname_db, QString port_db, QString conn_db, QString conn_name ) {
        db = QSqlDatabase::addDatabase(driver_db, conn_name);
        db.setUserName(user_db);
        db.setPassword(pass_db);
        db.setHostName(hostname_db);
        if (user_db != NULL) {
            db.setUserName(user_db);
        }
        if (pass_db != NULL) {
            db.setPassword(pass_db);
        }
        if (port_db != NULL) {
            //db.setPort(port_db);
        }
        if (conn_db != NULL) {
            db.setConnectOptions(conn_db);
        }
        switch (type_db)
        {
            case 0:
                // accesso a database QSLite
                db.setDatabaseName(path_and_name_db);
            break;
    
            case 1:
                // accesso a database MSACCESS .mdb
                db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=" + path_and_name_db);
            break;
    
            case 2:
                // accesso a database MSACCESS .accdb
                db.setDatabaseName("DRIVER=Provider=Microsoft.ACE.OLEDB.12.0;FIL={MS Access};DBQ=" + path_and_name_db);
            break;
    
            default:
                // accesso a database MSACCESS .mdb
                db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=" + path_and_name_db);
            break;
        }
        return db;
    }
    

    This is methode for open:

     bool DataBase::openDataBase(QSqlDatabase dbcheckopen) {
        if (dbcheckopen.isOpen()) {
            // first controll if is open already
            return true;
        } else {
            if(dbcheckopen.open()){
                return true;
            } else {
                return false;
            }
        }
    }
    

    Do you need other?



  • @elicat
    I could be wrong(! @SGaist ?), but you are passing QSqlDatabase dbcheckopen to your openDataBase function. That takes a copy of the parameter, and opens that. Shouldn't it be a QSqlDatabase& dbcheckopen?



  • @JonB said in Database result with connection name in all project:

    I could be wron

    for gestion of memory is sure is better but in function open i only check



  • @elicat said in Database result with connection name in all project:

    for gestion of memory is sure is better but in function open i only check

    Pardon?

    If you mean you only check whether it's already open, you don't, you call dbcheckopen.open() if it is not, it's your code....



  • QSqlDatabase dbstructure = QSqlDatabase::database("pippo");
    Although the connection is open and I do not have any error reports when I try to read a value of a selected record I do not return anything to the query.size () indicates me -1
            if(database->openDataBase(dbstructure)) {
                      .....
            }
    

    try this instead:

    QSqlDatabase dbstructure = QSqlDatabase::database("pippo");
    
            if(dbstructure.open()) {
                      .....
           }
    

    also include <QSqlError>
    and output

    qDebug() << querydb.lastError().text();
    //and
    qDebug() << dbstructure.lastError().text();
    

    on your failures to get more insight.


Log in to reply
 

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