Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2332
  • Last Modified:

Insert Binary Data (BLOB) From the Command Line

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
nathan8
Asked:
nathan8
1 Solution
 
andreifCommented:
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
 
nathan8Author Commented:
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now