QTSql Read row by row



  • I have a problem with following code:

    QSqlQuery query(db);
    QSqlRecord row = query.record();
    if (!query.exec("SELECT member FROM Database"))
    	qDebug() << "Error occurred while accessing table: " << query.lastError().text();
    if (row.count() > 0) {
    	for (int i = 0; i < row.count(); ++i) {
    		QString row_str = row.field(i).value().toString();
    		cout << i+1 << ". " << row_str.toStdString();
    	}
    }
    else
    	cout << "There are no members!\n";
    

    The member column contains 10 random names, but it prints one empty row. db is a mysql driver with opened connection to a database. Thanks for help.



  • I've found row.count() to be notoriously unreliable. I believe it's mentioned in the docs somewhere but I don't have a link handy.
    You should traverse by using query.next().



  • Could you give me an example? How to do that for any number of rows? I have no experience with QtSql and I can't figure it out.
    Edit:

    while (query.isValid()) {
    	QString row_str = query.nextResult();
    	cout << row_str.toStdString();
    }
    

    I have written this code, but it does not work.


  • Qt Champions 2017

    @ranger281
    Hi

       QSqlQuery query("SELECT country FROM artist");
        while (query.next()) {
            QString country = query.value(0).toString();
            doSomething(country);
        }
    

    http://doc.qt.io/qt-5/qsqlquery.html



  • How can I load the driver first and then execute the query?

            QSqlQuery query(db);
    	query = QSqlQuery("SELECT member FROM Database");
    	while (query.next()) {
    	        QString row = query.value(0).toString();
    		cout << row.toStdString();
    	}
    

    This is not working


  • Qt Champions 2017

    @ranger281

    you dont need to give it db as paramter.

    just open the database somewhere and u can use
    QSqlQuery in whole program.



  • In that case I get this error:

    QSqlQuery::exec: database not open
    

    db is already opened (i added db.open() before creating query).



  • @ranger281
    Where did you do @mrjj's "just open the database somewhere"?
    You've edited your post --- now show the code where you open the database and how it relates to where you execute the query. [BTW, did you check it actually opened correctly? QSqlDatabase::open() returns a value...]



  • @JonB I opened db in other function called from main and tried doing the same in this code, but it does not work.

    db.open();
    QSqlQuery query("SELECT member FROM Database");
    while (query.next()) {
    	QString row = query.value(0).toString();
    	cout << row.toStdString();
    }
    

  • Moderators



  • @jsulm This code returns 2:

    if (db.isOpen())
    	return 2;
    

    I will add the QSqlDatabase::open() in a moment.
    Edit:
    This code:

    	if (db.open())
    	        cout << "opened\n";
    	QSqlQuery query("SELECT member FROM Database");
    	while (query.next()) {
    		QString row = query.value(0).toString();
    		cout << row.toStdString();
    	}
    

    Returns:

    opened
    QSqlQuery::exec: database not open
    

    When I open the db only in the other function it returns the same error.


  • Lifetime Qt Champion

    Hi,

    You might want to consider printing the error if the open call failed rather than printing that message if it succeed.

    Also, where did you setup your database ? Can you show the related code ?



  •         QSqlDatabase db;
    	db = QSqlDatabase::addDatabase("QMYSQL", "SQLFileDatabase");
    	db.setHostName(QString::fromStdString(host));
    	db.setDatabaseName(QString::fromStdString(name));
    	db.setUserName(QString::fromStdString(user));
    	db.setPassword(QString::fromStdString(password));
    	db.open();
    

  • Lifetime Qt Champion

    Then check whether the open call is successful here and print the error if it's not the case.

    Also:

    • What version of Qt are you using ?
    • What platform ?
    • Did you install the MySQL client libraries ?


  • This post is deleted!


  • Change
    db = QSqlDatabase::addDatabase("QMYSQL", "SQLFileDatabase");

    to

    db = QSqlDatabase::addDatabase("QMYSQL");

    and everything should work.
    An explanation for using a default database can be found in the desricption of QSqlDatabase::addDatabase



  • The @Gerd 's solution works. Reading row by row too.
    Thank you all for help.


Log in to reply
 

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