Image insert in statically in database:
-
wrote on 12 Oct 2015, 06:01 last edited by
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.
-
Hi,
How are you inserting the image in the database ?
-
wrote on 13 Oct 2015, 10:16 last edited by
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;
} -
I'm not a SQLite expert, but maybe you have to commit the change?
-
How do you read the image back ?
-
wrote on 17 Oct 2015, 06:23 last edited by
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; } } }
}
-
Your select statement only asks for img, there's no id returned. And why the for loop ?
-
wrote on 20 Oct 2015, 05:38 last edited by
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.
-
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.
1/9