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. QTSql Read row by row
Forum Updated to NodeBB v4.3 + New Features

QTSql Read row by row

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 7 Posters 4.3k Views 2 Watching
  • 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.
  • ? A Former User

    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

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #6

    @ranger281

    you dont need to give it db as paramter.

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

    1 Reply Last reply
    1
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #7

      In that case I get this error:

      QSqlQuery::exec: database not open
      

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

      JonBJ 1 Reply Last reply
      0
      • ? A Former User

        In that case I get this error:

        QSqlQuery::exec: database not open
        

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #8

        @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...]

        ? 1 Reply Last reply
        1
        • JonBJ JonB

          @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...]

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #9

          @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();
          }
          
          jsulmJ 1 Reply Last reply
          0
          • ? A Former User

            @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();
            }
            
            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #10

            @ranger281 What does http://doc.qt.io/qt-5/qsqldatabase.html#open return?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            ? 1 Reply Last reply
            0
            • jsulmJ jsulm

              @ranger281 What does http://doc.qt.io/qt-5/qsqldatabase.html#open return?

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #11

              @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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #12

                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 ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #13
                          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();
                  
                  JonBJ 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #14

                    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 ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • ? A Former User
                              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();
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #15
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        Gerd
                        wrote on last edited by
                        #16

                        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

                        1 Reply Last reply
                        4
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #17

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

                          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