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. How to read QSqlQuery data from pointer
QtWS25 Last Chance

How to read QSqlQuery data from pointer

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.3k Views
  • 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.
  • S Offline
    S Offline
    SherifOmran
    wrote on last edited by
    #1

    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;
    

    }
    @

    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