Solved

Insert Binary Data (BLOB) From the Command Line

Posted on 2002-04-19
2
2,218 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

14 Experts available now in Live!

Get 1:1 Help Now