# Problems inserting PDF (Blob) to Mysql DB

**URL:** <https://www.ruby-forum.com/t/problems-inserting-pdf-blob-to-mysql-db/147374>\
**Category:** Ruby\
**Created:** [September 1, 2008, 5:57am UTC](https://www.ruby-forum.com/t/problems-inserting-pdf-blob-to-mysql-db/147374 "2008-09-01T05:57:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbermejo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rbermejo](https://www.ruby-forum.com/u/rbermejo)\
**Post date:** [September 1, 2008, 5:57am UTC](https://www.ruby-forum.com/t/problems-inserting-pdf-blob-to-mysql-db/147374/1 "2008-09-01T05:57:04Z")

</div>

Hi List /.

I need to insert a PDF file as a Blob to a Mysql DB.

I’m doing something like this:

f=File.open(x).read  
insert=%^INSERT INTO `main_recibos`  
( `recibo_id`, `sso` , `periodo` , `recibo` )  
VALUES  
( ‘#{recibo\_id}’ , ‘#{sso}’, #{periodo}, ‘#{f}’ ); ^  
@mysql.query(insert)

But I am getting ‘Please check the syntax of your SQL’  
And the error seems created by the funny chars on the binary data.  
I’ve already tried escaping in many ways the data with out success.

Any idea how to get rid of this error /?

Thank you

---

<div class="post-metadata">

**Author:** ![rbermejo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rbermejo](https://www.ruby-forum.com/u/rbermejo)\
**Post date:** [September 1, 2008, 11:10am UTC](https://www.ruby-forum.com/t/problems-inserting-pdf-blob-to-mysql-db/147374/2 "2008-09-01T11:10:04Z")

</div>

Hi Rodrigo,

Are you able to try the SQL directly against your MySQL db for a test ?  
Might be worth doing something like:

1. First just confirm you are able to insert a simple ‘text’ file into  
the DB using this sql. (…‘recbibo’)…VALUES (…'This is my text  
file\ndoes this work?\n");

2. Then move onto a (small) PDF (or other binary) file: perhaps ‘dress’  
up the binary file (edit it like a text file) and add the SQL round it ?

I’m just wondering whether this is really a Ruby issue or not ? (more to  
do with mysql SQL syntax ?)

Cheers

John

Rodrigo B. wrote:

> Hi List /.
> 
> I need to insert a PDF file as a Blob to a Mysql DB.
> 
> I’m doing something like this:
> 
> f=File.open(x).read  
> insert=%^INSERT INTO `main_recibos`  
> ( `recibo_id`, `sso` , `periodo` , `recibo` )  
> VALUES  
> ( ‘#{recibo\_id}’ , ‘#{sso}’, #{periodo}, ‘#{f}’ ); ^  
> @mysql.query(insert)
> 
> But I am getting ‘Please check the syntax of your SQL’  
> And the error seems created by the funny chars on the binary data.  
> I’ve already tried escaping in many ways the data with out success.
> 
> Any idea how to get rid of this error /?
> 
> Thank you
