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. QSqlQuery error
Qt 6.11 is out! See what's new in the release blog

QSqlQuery error

Scheduled Pinned Locked Moved Solved General and Desktop
qsqlerror
3 Posts 2 Posters 2.1k Views 1 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I've spent quite some time trying to figure out what's wrong with this code. Whenever I run it I get the following messages:
    The database is open!
    QSqlQuery::value: not positioned on a valid record

    The code:

        QSqlDatabase db;
        db = QSqlDatabase::addDatabase ("QSQLITE");
        db.setDatabaseName ("C:/Programming/Qtsamples/Image_from_DB/db.db");
    	db.open ();
    
        QSqlQuery query;
    
        if(!db.open ())
        {
            qDebug() << "The database is NOT open!";
        }
        else
        {
            qDebug() << "The database is open!";
        }
    
        query.prepare ("SELECT Pic FROM Items");
        query.exec ();
    
    
    
        query.first ();
    	QByteArray ByteArray;
    	ByteArray = query.value (1).toByteArray ();
        QPixmap Pixmap = QPixmap();
        Pixmap.loadFromData (ByteArray);
        db.close ();
    
    	ui->label->setPixmap (Pixmap);
        ui->label->show ();
    

    Please help me to make it positioned on a valid record!
    Thank you.

    kshegunovK 1 Reply Last reply
    0
    • G gabor53

      Hi,
      I've spent quite some time trying to figure out what's wrong with this code. Whenever I run it I get the following messages:
      The database is open!
      QSqlQuery::value: not positioned on a valid record

      The code:

          QSqlDatabase db;
          db = QSqlDatabase::addDatabase ("QSQLITE");
          db.setDatabaseName ("C:/Programming/Qtsamples/Image_from_DB/db.db");
      	db.open ();
      
          QSqlQuery query;
      
          if(!db.open ())
          {
              qDebug() << "The database is NOT open!";
          }
          else
          {
              qDebug() << "The database is open!";
          }
      
          query.prepare ("SELECT Pic FROM Items");
          query.exec ();
      
      
      
          query.first ();
      	QByteArray ByteArray;
      	ByteArray = query.value (1).toByteArray ();
          QPixmap Pixmap = QPixmap();
          Pixmap.loadFromData (ByteArray);
          db.close ();
      
      	ui->label->setPixmap (Pixmap);
          ui->label->show ();
      

      Please help me to make it positioned on a valid record!
      Thank you.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @gabor53
      Hello,
      You select one column from the table, but try to retrieve the second column from the resultset, try:

          query.value(0).toByteArray();
      

      Additional (potential) problems exist in your code:

      • In example, you don't handle the return value of your QSqlDatabase::first call, it's supposed to tell you whether you're properly positioned at the first record or not.
      • You call QSqlDatabase::open twice, you can use QSqlDatabase::isOpen to check if your database was properly opened instead of trying to open it again.
      • You don't handle the return value of QSqlQuery::exec as well.
      • There is no need to really prepare the query before execution, you don't intend to use it multiple times with different bindings.
      • You close your database after executing the query. Usually the idea is to open/close the database once and have multiple queries executed on the same database instance.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Thank you. it worked.

        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