How to insert BLOB and CLOB files in MySQL?

user1411472 picture user1411472 · May 24, 2012 · Viewed 84.8k times · Source

I want to store images and .docx/.doc, .pptx/.ppt, .pdf files using the front end of my software. I don't understand how to implement this and how to insert the BLOB and CLOB files into the table. Please help.

I am using Kubuntu 11.04, MySQL5, Qt 4.7.3.

Answer

Devart picture Devart · May 24, 2012

Two ways:

1 - Use a LOAD_FILE function -

INSERT INTO table1 VALUES(1, LOAD_FILE('data.png'));

2 - Insert file as hex string, e.g. -

INSERT INTO table1 VALUES 
  (1, x'89504E470D0A1A0A0000000D494844520000001000000010080200000090916836000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000001E49444154384F6350DAE843126220493550F1A80662426C349406472801006AC91F1040F796BD0000000049454E44AE426082');