PDA

View Full Version : Read image and save to database?



Gokulnathvc
15th July 2011, 14:12
I am using the following code to take screenshot and saving it in the hard disk, i want to read the file and save it into the blob field of the mysql database. How to connect to the particular database,table and field. Please help me with my code.


QScreenShot::QScreenShot(QWidget *parent) :
QDialog(parent),
ui(new Ui::QScreenShot)
{
ui->setupUi(this);
c=0;
bool ok=false;
QDateTime dateTime = QDateTime::currentDateTime();
QString dateTimeString = dateTime.toString();

QDir().mkdir("D:\\QtImages\\");
QDir().setCurrent("D:\\QtImages\\");
QDir().mkdir("ScreenShots");
QDir().setCurrent("ScreenShots");
QDir().mkdir(dateTimeString);

QMessageBox::warning(NULL,"DriveList",dateTimeString,QMessageBox::Ok);

QStringList list=QSqlDatabase::drivers();
QString driveList;
for(int i=0;i<list.length();i++)
{
driveList += list[i];
}
//QMessageBox::warning(NULL,"DriveList",driveList,QMessageBox::Ok);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={MYSQL ODBC 3.51 Driver};FIL={MYSQL};DBQ=screengrabber");
db.setHostName("localhost");
db.setConnectOptions("CLIENT_ODBC");
//db.setDatabaseName("screengrabber");
db.setUserName("root");
db.setPassword("1");
ok = db.open();

for(;;)
{

originalPixmap =QPixmap();
originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
QString strfname;
strfname.sprintf("%d",c);
originalPixmap.save("D:\\image"+strfname+".jpeg","jpeg");
c++;
char Data;
QFile file("D:\\image"+strfname+".jpeg");
file.open(QIODevice::ReadOnly);
file.seek(0);
QByteArray buf;
buf=file.read(250000);
QSqlQuery query;
query.prepare("INSERT INTO log VALUES('grab_date='2011-04-26 15:55:09',ip_address='172.16.0.51',image=:val');");
query.bindValue ( ":val", buf);
QString strquery;
strquery = "INSERT INTO log (grab_date,ip_address,image) VALUES ( '";
strquery += '2011-04-26 15:55:09';
strquery += "' , '";
strquery += '172.16.0.51';
strquery += "' , ";
strquery += buf;
strquery += " );";

//QMessageBox::warning(NULL,"DriveList",strquery,QMessageBox::Ok);

QSqlQuery insertQuery( strquery, db);
bool done = insertQuery.exec();

QFile newfile("D:\\img.txt");
newfile.open(QIODevice::WriteOnly);
newfile.write(buf);

int iSecret, iRandom;
iSecret = rand() % 20 + 1;
iRandom=iSecret*60000;

QMutex mutex;
mutex.lock();
QWaitCondition waitCondition;
waitCondition.wait(&mutex, iRandom);
mutex.unlock();

}

}

mcosta
15th July 2011, 14:26
Use QSqlQuery::prepare() and QSqlQuery::bindValue() to insert blob's.

Why do you use QODBC driver and not the native QMYSQL???

Gokulnathvc
15th July 2011, 14:38
I am a beginner, I just want to take screenshots and open the database and save the image's blob data into the field. Could you please help me in modifying my code in order to achieve this. This application connects the server database of the office. Will it be possible. please help me giving the modified code of my block .. thank you

Gokulnathvc
16th July 2011, 11:25
I have tried using the native QMYSQL also. but it failed to connect.

ChrisW67
17th July 2011, 00:10
It is plain that you are expecting someone here to your job for you, and you are clearly disappointed that this has not happened. It should be apparent that no-one here is going to write the complete solution for you.

Your truly appalling grasp of SQL has been corrected before, as has nearly every single aspect of this problem (http://www.qtcentre.org/search.php?do=finduser&userid=26040&starteronly=1&contenttype=vBForum_Thread). This thread repeats the general answer for accessing BLOB fields from databases. You have been given direct links to the relevant pages of the Qt documentation on the problem of manipulating a file in-memory without a temporary file on disc. You have been given nudges in the right direction. You have been given links to open source project that do similar things. We simply cannot help someone that cannot be bothered to take the very advice that they are asking for.

How to take screenshots and save the binary data in the blob field? points you back to numerous previous instances of asking for, and utterly ignoring, advice on this same problem. You need to build and install the MySQL database plugin before you can connect to a MySQL database using the native driver. You have been told this before but clearly haven't bothered.

Gokulnathvc
21st July 2011, 14:34
You are good in researching others work flow.:D

ChrisW67
22nd July 2011, 01:18
... and you, Sir, are very good at ignoring all advice and continuing to ask the same question yet again (http://www.qtcentre.org/threads/24261-how-to-save-a-image-file-(-say-png-)-into-mysql-database?p=197827#post197827) (resurrecting a two year old thread for no reason) with no evidence of any effort on your part to take on advice already offered.

Here is the summary:

Stop using obsolete, deprecated or unsafe methods, e.g. QString::sprintf(), when better alternatives exist
Use QBuffer and QByteArray to save an image to an in-memory buffer (no temporary disk file).
Learn some basic SQL. If you cannot construct a basic SQL query outside Qt/C++ then you will not have any luck with QSqlQuery.
Use QSqlQuery::prepare() and QSqlQuery::bindValue() to insert blob's. In fact, you should use this anywhere you are tempted to build a query from many parts into a string.
You need to build and use the MySQL native driver to access your database directly rather than through ODBC.