Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Image insert in statically in database:
QtWS25 Last Chance

Image insert in statically in database:

Scheduled Pinned Locked Moved Mobile and Embedded
9 Posts 3 Posters 3.0k 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.
  • K Offline
    K Offline
    karthi shan
    wrote on last edited by
    #1

    Hi All,

    How to image insert statically in the sqlite database. i can able to insert dynamically only. If i given image path, at run time path(string) only came, image doesn't appeared. anyone help me.

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

      Hi,

      How are you inserting the image in the database ?

      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
      • K Offline
        K Offline
        karthi shan
        wrote on last edited by
        #3

        Hi,

        Here is image inserting code, I need to image insert in the Sqlite table, how to image insert directly in the Sqlite table.

        QFile file("/home/Desktop/image/food.png");
        if(myDB.open())//opening database
        {
        PRINT <<"Database Opened...";
        if (!file.open(QIODevice::ReadOnly)) return;
        QByteArray byteArray = file.readAll();
        QSqlQuery qry;
        qry.exec("CREATE TABLE images(id integer primary key autoincrement, img BLOB);");
        {
        PRINT <<"images Table Created...";
        }
        QSqlQuery query;
        query.prepare("INSERT INTO images(img) VALUES (?)");
        query.addBindValue(byteArray);
        query.exec();
        PRINT <<"values inserted...";
        qDebug()<<byteArray;
        }

        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I'm not a SQLite expert, but maybe you have to commit the change?

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

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

            How do you read the image back ?

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

            K 1 Reply Last reply
            0
            • SGaistS SGaist

              How do you read the image back ?

              K Offline
              K Offline
              karthi shan
              wrote on last edited by
              #6

              @SGaist

              Hi,
              Here image read code is here:

              void MainWindow::on_pushButton_3_clicked()
              {
              int id;

              if(myDB.open())//opening database
              {
                  QByteArray array;
                  QSqlQuery query("SELECT img FROM images");
                  while(query.next())
                  {
                      for( int i =1; i <= 2; i++ )
                      {
                          array = query.value(1).toByteArray();
                          qDebug()<<0;
                          id=query.value(0).toInt();
                          qDebug()<<array;
                          QPixmap pixmap = QPixmap();
                          pixmap.loadFromData(array);
                          if (id==11)
                          {
                              ui->label_2->setPixmap(pixmap);
                          }
                          if(id==10)
                          {
                              ui->label_3->setPixmap(pixmap);
                          }
                          if(id==12)
                          {
                              ui->label->setPixmap(pixmap);
                          }
                          if(id==11)
                          {
                              ui->label_5->setPixmap(pixmap);
                          }
              
                          ui->label_4->setNum(id);
                          qDebug()<<id;
                      }
                  }
              }
              

              }

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

                Your select statement only asks for img, there's no id returned. And why the for loop ?

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

                K 1 Reply Last reply
                0
                • SGaistS SGaist

                  Your select statement only asks for img, there's no id returned. And why the for loop ?

                  K Offline
                  K Offline
                  karthi shan
                  wrote on last edited by
                  #8

                  @SGaist

                  Image insert and fetching its working, But at the time i can able to store one image only. i need to store multiple image at the time. single image insert and fetching its working fine.

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

                    If you want to insert several images, you need to create a list of image, loop through it to insert them.

                    If you want to show them all at once, then create as many QLabel you need in the loop reading the results.

                    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

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved