# Insert marshall'd data into mysql

**URL:** <https://rubytalk.org/t/insert-marshalld-data-into-mysql/38587>\
**Category:** ruby-talk\
**Created:** [25 June 2007 00:08 UTC](https://rubytalk.org/t/insert-marshalld-data-into-mysql/38587 "2007-06-25T00:08:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Aaron\_Smith](https://rubytalk.org/u/Aaron_Smith)\
**Post date:** [25 June 2007 00:08 UTC](https://rubytalk.org/t/insert-marshalld-data-into-mysql/38587/1 "2007-06-25T00:08:37Z")

</div>

Hey all.. what methods can I use to safely insert Ruby marshall'd data  
into mysql?

Thanks  
Aaron

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Peter\_Cooper](https://avatars.discourse-cdn.com/v4/letter/p/db5fbb/32.png) [@Peter\_Cooper](https://rubytalk.org/u/Peter_Cooper)\
**Post date:** [25 June 2007 00:16 UTC](https://rubytalk.org/t/insert-marshalld-data-into-mysql/38587/2 "2007-06-25T00:16:03Z")

</div>

Use a BLOB column. MySQL treats BLOB columns as 8 bit binary data.  
TEXT will also work, but there are a number of considerations (  
[http://dev.mysql.com/doc/refman/5.0/en/blob.html](http://dev.mysql.com/doc/refman/5.0/en/blob.html) ). Since Marshalled  
data is just regular 8 bit data, you can just throw it in there. If  
you were /really/ worried / paranoid about throwing full 8 bit data,  
non printable characters and all, into a database, you could Base64  
encode and decode.

Cheers,  
Peter Cooper

> **[Ruby Inside: The Ruby Blog](http://www.rubyinside.com/)**
>
> The biggest Ruby and Rails blog since 2006 - tutorials, news, and lots more

> **···**
>
> On 6/25/07, Aaron Smith \<beingthexemplary@gmail.com\> wrote:
> 
> > Hey all.. what methods can I use to safely insert Ruby marshall'd data  
> > into mysql?

---

<div class="post-metadata">

**Author:** ![Aaron\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Aaron\_Smith](https://rubytalk.org/u/Aaron_Smith)\
**Post date:** [25 June 2007 00:24 UTC](https://rubytalk.org/t/insert-marshalld-data-into-mysql/38587/3 "2007-06-25T00:24:47Z")

</div>

Peter Cooper wrote:

> **···**
>
> > On 6/25/07, Aaron Smith \<beingthexemplary@gmail.com\> wrote:
> > 
> > > Hey all.. what methods can I use to safely insert Ruby marshall'd data  
> > > into mysql?
> > 
> > Use a BLOB column. MySQL treats BLOB columns as 8 bit binary data.  
> > TEXT will also work, but there are a number of considerations (  
> > [http://dev.mysql.com/doc/refman/5.0/en/blob.html](http://dev.mysql.com/doc/refman/5.0/en/blob.html) ). Since Marshalled  
> > data is just regular 8 bit data, you can just throw it in there. If  
> > you were /really/ worried / paranoid about throwing full 8 bit data,  
> > non printable characters and all, into a database, you could Base64  
> > encode and decode.
> > 
> > Cheers,  
> > Peter Cooper  
> > [http://www.rubyinside.com/](http://www.rubyinside.com/)
> 
> Makes sense with blobs. What about in the actual insert statments? EX:
> 
> someObj= {:one =\> 'two', :three =\> 'four'}  
> d = Marshal.dump(someObj)  
> @con.query("INSERT INTO user\_data SET marshal='#{d}'");
> 
> That will produce an error because of some of the characters in the  
> marshal string..
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Peter\_Cooper](https://avatars.discourse-cdn.com/v4/letter/p/db5fbb/32.png) [@Peter\_Cooper](https://rubytalk.org/u/Peter_Cooper)\
**Post date:** [25 June 2007 02:09 UTC](https://rubytalk.org/t/insert-marshalld-data-into-mysql/38587/4 "2007-06-25T02:09:17Z")

</div>

Use parameter binding. Not sure what library you're using, but with  
Ruby DBI it'd be something like this:

@handle.do("INSERT INTO user\_data SET marshal=?", d)

See if the same works on your setup. Parameter binding should be  
supported by all decent database libraries, so there'll be a way to do  
it. If not, then there has to be an escaping/quotation method to get  
the data in 😉

Cheers,  
Peter Cooper

> **[Ruby Inside: The Ruby Blog](http://www.rubyinside.com/)**
>
> The biggest Ruby and Rails blog since 2006 - tutorials, news, and lots more

> **···**
>
> On 6/25/07, Aaron Smith \<beingthexemplary@gmail.com\> wrote:
> 
> > Peter Cooper wrote:  
> > \> On 6/25/07, Aaron Smith \<beingthexemplary@gmail.com\> wrote:  
> > \>\> Hey all.. what methods can I use to safely insert Ruby marshall'd data  
> > \>\> into mysql?  
> > \>  
> > \> Use a BLOB column. MySQL treats BLOB columns as 8 bit binary data.  
> > \> TEXT will also work, but there are a number of considerations (  
> > \> [http://dev.mysql.com/doc/refman/5.0/en/blob.html](http://dev.mysql.com/doc/refman/5.0/en/blob.html) ). Since Marshalled  
> > \> data is just regular 8 bit data, you can just throw it in there. If  
> > \> you were /really/ worried / paranoid about throwing full 8 bit data,  
> > \> non printable characters and all, into a database, you could Base64  
> > \> encode and decode.  
> > \>  
> > \> Cheers,  
> > \> Peter Cooper  
> > \> [http://www.rubyinside.com/](http://www.rubyinside.com/)
> > 
> > Makes sense with blobs. What about in the actual insert statments? EX:
> > 
> > someObj= {:one =\> 'two', :three =\> 'four'}  
> > d = Marshal.dump(someObj)  
> > @con.query("INSERT INTO user\_data SET marshal='#{d}'");
> > 
> > That will produce an error because of some of the characters in the  
> > marshal string..

---

<div class="post-metadata">

**Author:** ![Aaron\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Aaron\_Smith](https://rubytalk.org/u/Aaron_Smith)\
**Post date:** [25 June 2007 02:13 UTC](https://rubytalk.org/t/insert-marshalld-data-into-mysql/38587/5 "2007-06-25T02:13:02Z")

</div>

Peter Cooper wrote:

> **···**
>
> > On 6/25/07, Aaron Smith \<beingthexemplary@gmail.com\> wrote:
> > 
> > > \> non printable characters and all, into a database, you could Base64  
> > > @con.query("INSERT INTO user\_data SET marshal='#{d}'");
> > > 
> > > That will produce an error because of some of the characters in the  
> > > marshal string..
> > 
> > Use parameter binding. Not sure what library you're using, but with  
> > Ruby DBI it'd be something like this:
> > 
> > @handle.do("INSERT INTO user\_data SET marshal=?", d)
> > 
> > See if the same works on your setup. Parameter binding should be  
> > supported by all decent database libraries, so there'll be a way to do  
> > it. If not, then there has to be an escaping/quotation method to get  
> > the data in 😉
> > 
> > Cheers,  
> > Peter Cooper  
> > [http://www.rubyinside.com/](http://www.rubyinside.com/)
> 
> I'm using the mysql library for Ruby. Not sure about the paramter  
> binding.. but Base64.b64encode and Base64.decode64 works fine.
> 
> Thanks
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
