How to read QSqlQuery data from pointer



  • Hello guys,

    I wrote this code to read data returned from the sql query. Inside the QuerydB fn, data is read correct however in the main, it returns empty. Any idea?

    @
    MainForm::MainForm(QWidget *parent) :
    QMainWindow(parent,Qt::MSWindowsFixedSizeDialogHint),
    ui(new Ui::MainForm)

    {

    QString SettingFile = qApp->property("SettingFile").toString();
    
    
    QString query="SELECT dBpath FROM setting";
    QSqlQuery queryop;
    int r;
    qDebug() << "queryop& " << &queryop;
    QuerydB(query,qApp->property("SettingFile").toString(),&queryop,&r);
    

    // Load database Setting file path from dB and check their existance
    qDebug() << "length of query is " << r;
    qDebug() << "queryop& " << &queryop;
    while (queryop.next())
    {
    QString Stored_dBPath = queryop.value(0).toString();
    Stored_dBPath = Stored_dBPath +qApp->property("SettingFileName").toString();
    qDebug()<< "loop" << Stored_dBPath;
    bool dBFileExist=QFile::exists(Stored_dBPath);
    if (dBFileExist) { ui->logindBList->addItem(Stored_dBPath); }
    }

    }
    @

    @
    void MainForm::QuerydB(QString Query, QString dataBaseFile, QSqlQuery *result_p, int numRows)
    {
    /
    Query: SQL query ex: insert, ... etc
    dataBaseFile: filename including path to the database
    result: pointer to the QSqlQuery that holds the data
    lens: pointer to the number of rows in the output data
    */

    dataBaseFile = QDir::toNativeSeparators(dataBaseFile);
    database = new QSqlDatabase();
    //set database driver to QSQLITE
    *database = QSqlDatabase::addDatabase("QSQLITE");
    //dbname=qApp->property("SettingFile").toString();
    database->setDatabaseName(dataBaseFile);
    
    if(!database->open())
    {
        QMessageBox::warning(0,"Error","Couldn't open setting database");
        return;
    }
    
    
     // read data
    QSqlQuery result (Query);
    

    // result.prepare(Query);
    // result.exec ();
    int r=0;
    qDebug() << "result& " << &result;

    result_p=&result;
    
    while (result.next())
    {
        QString Stored_dBPath = result.value(0).toString();
        qDebug()<< "fn" << Stored_dBPath;
        r++;
    }
    result.first();
    
     //close database;
    // QString connname = database->connectionName();
    
    
     database->close();
     database->removeDatabase(dataBaseFile);
     delete database;
    
      *numRows = r;
    
    
     return;
    

    }
    @


Log in to reply
 

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