Image insert in statically in database:



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


  • Lifetime Qt Champion

    Hi,

    How are you inserting the image in the database ?



  • 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;
    }


  • Moderators

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


  • Lifetime Qt Champion

    How do you read the image back ?



  • @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;
            }
        }
    }
    

    }


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    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.


Log in to reply
 

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