PDA

View Full Version : How to save the image blob data into the database??



Gokulnathvc
4th August 2011, 13:25
I am using the following code:
I am reading the jpeg image in Qbytearray; But while saving to database, the image size is doubled and while retrieving from database, the file is being corrupted. How to read the image properly??


QByteArray buf=NULL;
originalPixmap =QPixmap();
originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
QString strfname;
strfname.sprintf("%d",c);
originalPixmap.save("C:\\Program Files\\image"+strfname+".jpeg","jpeg");
c++;
char Data;
QFile file("C:\\Program Files\\image"+strfname+".jpeg");
file.open(QIODevice::ReadOnly);
int len=file.size();
//buf=file.read(len);
buf=file.readAll();
file.close();
file.remove();
QSqlQuery query;
query.exec("INSERT INTO log (grab_date, ip_address, image,logged_user) "
"VALUES (?,?,?,?)");
query.bindValue(0,dd);
query.bindValue(1,address);
query.bindValue(2,buf.toBase64());
query.bindValue (3,hostname);
bool qry=query.exec();

FelixB
4th August 2011, 13:44
please post in one of your 1214234 other thread on this topic.

please stop creating new threads to the same topics again and again.

Gokulnathvc
4th August 2011, 14:01
Please try to fix my problem

wysota
4th August 2011, 14:54
Learn what base64 is and how it works. And stop spawning threads on the same subject, this is really annoying.

Gokulnathvc
5th August 2011, 13:38
I have tried using base64, still while saving in the database, the file size increases. but the length of the byte array remains same, but saving in the database, it increases

Added after 1 55 minutes:

QFile file("C:\\Program Files\\image"+strfname+".jpeg");
file.open(QIODevice::ReadOnly);
int len=file.size();
buf=file.readAll()

Here buf contains the blob data, its length is equal to filesize. But while executing the query. its size increases upto 60% and while seeing in database. What's wrong with my code?? Please help me??

Please dont think me that am re-posting again and again.... Its very hard fro me to find the error. And found nothing in all sites. Please help me to rectify this?

wysota
5th August 2011, 14:00
I have tried using base64, still while saving in the database, the file size increases.
That's totally expected, isn't it?


Please dont think me that am re-posting again and again....
We don't have to think. There is a nice button "find all posts of this user" (or alike) so I can see you're spawning many threads on the same problem. Please choose one of the threads and restrict yourself to posting only there.


Its very hard fro me to find the error. And found nothing in all sites. Please help me to rectify this?
You need to understand some things. For instance you need to understand why base64 increases volume of the data. For that you need to know how base64 works. If you don't and you're not willing to do the research then don't use it.

Gokulnathvc
5th August 2011, 14:26
I have tried without converting to base64 also, the same problem exists, i just want only the blob data and not any other coded information with it. Could you please help me with my code..

wysota
5th August 2011, 14:29
There is nothing wrong with the code you posted apart from the fact that you needlessly save the image to a file.

Gokulnathvc
5th August 2011, 14:33
I have used the following code which directly gets the blob data from the pixmap.But in database, blob size increases to about 60%.

QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
originalPixmap.save(&buffer, "jpeg");

For instance, the actual file size is 135 kb but, in database it has been stored as 267 kb.

wysota
5th August 2011, 16:11
And what's wrong with that?

Gokulnathvc
6th August 2011, 05:38
While retrieving the image i cant able to reproduce it, it says the file has been damaged or corrupted.

wysota
6th August 2011, 07:31
How are you retrieving the file? And what's your table schema?

Gokulnathvc
6th August 2011, 08:49
Retrieving the file from database is being done by my colleague which is in C#. He is just getting the bytes from the table and then reproducing it. My table schema is:

Ip_address(varchar) , grab_date (datetime), image(longblob).

I am just reading the pixmap and saving the binary data into the QByteArray and then saving it into the database. You can check with the code pasted above..

wysota
6th August 2011, 08:55
Retrieving the file from database is being done by my colleague which is in C#. He is just getting the bytes from the table and then reproducing it.
Does he do it according to the same rules you use when storing the file? Because if you use base64 and he doesn't then it has no chance of working, you know.

Gokulnathvc
6th August 2011, 08:57
If i am saving as a normal image and not as base 64, how could he retreive the image, as it contains some coded information as the size increases?

thefatladysingsopera
6th August 2011, 09:20
I am a reading this book http://www.apress.com/9781590598313 (http://www.apress.com/9781590598313)and although i have not reached the part on saving images,it will help you a little i guess.

wysota
6th August 2011, 10:03
If i am saving as a normal image and not as base 64, how could he retreive the image, as it contains some coded information as the size increases?

I have no idea what you mean. But the general rule is that if you speak russian and he speaks chinese, you won't understand each other. You need to agree to a specific algorithm and stick with it, both of you. And make sure you store the blob according to MySQL rules, I'm not sure it will accept binary data through a regular textual insert call although it's been very long since I last used MySQL.

DanH
6th August 2011, 22:21
You're saving the data in base64. As I understand it, there should be no need to do that if the field is defined as a blob.

If you saved it in base64, though, you need to translate it out of base64 on retrieval. Eg, use QByteArray.fromBase64.

Gokulnathvc
8th August 2011, 08:14
If i save the image just as QByteArray, How it should be retrieved??

wysota
8th August 2011, 09:11
The same way, as a byte array. Just make sure the database doesn't perform any conversions when you store the data.