Solved

Insert Binary Data (BLOB) From the Command Line

Posted on 2002-04-19
2
2,207 Views
Last Modified: 2012-06-22
I have a binary data file (A Quicktime Movie, to be precise) that I would like to load into a field in an MySQL table.  I can do this easily in PHP, but this time I want to do it from the command line.

What I want is, and this is pseudocode, something like:

INSERT INTO myTable (binary_field, title) VALUES (read_file('movie.mov'), 'My Movie');

Is there anything like this?  I want the actual binary contents of the file to go into the BLOB, not the filename.  I'm probably missing something very simple.
0
Comment
Question by:nathan8
2 Comments
 
LVL 6

Accepted Solution

by:
andreif earned 200 total points
ID: 6954635
Yes, you can use LOAD_FILE() function. Here is the text from manual:

LOAD_FILE(file_name)

Reads the file and returns the file contents as a string. The file must be on the server, you must specify the full pathname to the file, and you must have the file privilege. The file must be readable by all and be smaller than max_allowed_packet. If the file doesn't exist or can't be read due to one of the above reasons, the function returns NULL:
mysql> UPDATE table_name
       SET blob_column=LOAD_FILE("/tmp/picture")
       WHERE id=1;

If you are not using MySQL Version 3.23, you have to do the reading of the file inside your application and create an INSERT statement to update the database with the file information. One way to do this, if you are using the MySQL++ library, can be found at http://www.mysql.com/documentation/mysql++/mysql++-examples.html

0
 
LVL 1

Author Comment

by:nathan8
ID: 6955111
Thank you!  I actually figured that out after reading through the miscellaneuos functions section more closely, but your example was very helpful.  Terrific work!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now