model->setTable("photos"); model->setHeaderData(0, Qt::Horizontal, "Id"); model->setHeaderData(1, Qt::Horizontal, "Photo"); model->setHeaderData(2, Qt::Horizontal, "File_Name"); model->setHeaderData(3, Qt::Horizontal, "Size"); model->setHeaderData(4, Qt::Horizontal, "Floor"); model->setHeaderData(5, Qt::Horizontal, "Order1");
void dbCreate::downloadData(QString &file, QString &floor, QString &order){ QSqlQuery query(db); QFileInfo name = file; QFile inFile(file); if(!inFile.open(QIODevice::ReadOnly)) qDebug() << "error open file"; QByteArray photo = inFile.readAll(); quint64 size = inFile.size(); QString sizeMKB; QString MKB[3] = {"MB", "KB", "B"}; if(size > 1024*1024 - 1) sizeMKB = QString::number(qreal(size) / (1024*1024), 'f', 1) + " " + MKB[0]; else if(size > 1023) sizeMKB = QString::number(qreal(size) / 1024, 'f', 1) + " " + MKB[1]; else if(size < 1024) sizeMKB = QString::number(qreal(size)) + " " + MKB[2]; if(query.prepare("INSERT INTO photos (Photo, File_Name, Size, Floor, Order1) VALUES (:Photo, :File_Name, :Size, :Floor, :Order1)")) { query.bindValue(":Photo", photo); query.bindValue(":File_Name", name.fileName()); query.bindValue(":Size", sizeMKB); query.bindValue(":Floor", floor); query.bindValue(":Order1", order); if(query.exec()) model->select(); else QMessageBox::critical(0, QString::number(query.lastError().type()), query.lastError().text()); } else qDebug() << query.lastError().text();}
void dbCreate::writeData(int Scene_Id){ QSqlQuery query(db); query.prepare("SELECT Photo FROM photos WHERE Id = :Id"); query.bindValue(":Id", Scene_Id); query.exec(); if(query.first()) { QByteArray photo = query.value(1).toByteArray(); QFile filePhoto("123.jpg"); // имя все время одно и тоже пока, но это не важно.... if(filePhoto.open(QIODevice::WriteOnly)) filePhoto.write(photo); else qDebug() << "error"; } else qDebug() << query.lastError().type() << query.lastError().text(); }
QSqlQuery::value: not positioned on a valid record
QString dbCreate::writeData(int Scene_Id){ QSqlQuery query(db); query.prepare("SELECT Photo FROM photos WHERE Id = :Id"); query.bindValue(":Id", Scene_Id); if(query.exec()) { while(query.next()) { QByteArray photo = query.value(0).toByteArray(); QFile filePhoto(QString::number(Scene_Id) + ".jpg"); if(filePhoto.open(QIODevice::WriteOnly)) { filePhoto.write(photo); return filePhoto.fileName(); } else qDebug() << "error"; } } else qDebug() << query.lastError().type() << query.lastError().text(); return QString();}