Saving binary data in mysql

I have a PDF file on my local machine. I want to load this file in a BINARY BLOB in an SQL database. Other approaches mentioned here [ Binary data in MySQL uses PHP. I want an easy way to load this pdf file on a Linux command line. Unfortunately, I do not have access to the remote file system, so I can’t just save the file links, as mentioned elsewhere ... I need to use this MySQL database as a virtual file system for these PDF files.

From the PhP example, it seems all that is required is to avoid a slash before using the INSERT command? Is there an easy way to achieve this on the Linux command line?

+5
source share
4 answers

Not sure if this completely solves my problem, but I found that the file system level is implemented on top of MySQL. I think I can use this to automatically save my PDF files as a BLOB ... I still need to figure out how to store the keys in the PDF file in this file system for structured access to the PDF file based on something more meaningful than < inode, seq>.

http://sourceforge.net/projects/mysqlfs/ and
http://www.linux.com/archive/feature/127055

0
source

You can use the mysql LOAD_FILE function in combination with a little shellscript to do this, I think.

Unconfirmed code follows:

#!/bin/bash

if [ -z $1 ]
then 
    echo "usage: insert.sh <filename>"
else
    SQL="INSERT INTO file_table (blob_column, filename) VALUES(LOAD_FILE('$1'), '$1')"

    echo "$SQL" > /tmp/insert.sql
   cat /tmp/insert.sql | mysql -u user -p -h localhost db
fi

And you can use it as follows:

<prompt>./insert.sh /full/path/to/file

, tempfile, . , LOAD_FILE() FILE MySQL .

+4

curl POST, . , , curl.

+1

All Articles