# Saving gzipped string into Sqlite3 via Rails throws unrecognized token error

**URL:** <https://www.ruby-forum.com/t/saving-gzipped-string-into-sqlite3-via-rails-throws-unrecognized-token-error/185794>\
**Category:** Ruby\
**Created:** [March 26, 2010, 6:30am UTC](https://www.ruby-forum.com/t/saving-gzipped-string-into-sqlite3-via-rails-throws-unrecognized-token-error/185794 "2010-03-26T06:30:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablitoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b487fb/32.png) [@pablitoman](https://www.ruby-forum.com/u/pablitoman)\
**Post date:** [March 26, 2010, 6:30am UTC](https://www.ruby-forum.com/t/saving-gzipped-string-into-sqlite3-via-rails-throws-unrecognized-token-error/185794/1 "2010-03-26T06:30:58Z")

</div>

Not sure if this belongs in the rails forum or here (it’s a little of  
both)…

I’m using rails 2.3 and I’m trying to compress (gzip) text that I’d  
like to save using ActiveRecord into a sqlite database.

However, the compressed text isn’t being saved b/c I get this type of  
error: SQLite3::SQLException: unrecognized token: "'x##UËŽ#7

Any thoughts on what I can do to avoid this error?

should I compress the text in some other fashion?  
should I save the data using some other method(s)?  
TIA!!!  
Relevant code is below.

###########################################

# compress my text

require ‘zlib’  
defl = Zlib::Deflate  
test\_string = “

### some text
some additional text

here’s some  
more text

”  
compressed\_string = defl.deflate(test\_string)  
=\> “x\234\263\3110\266+\316\317MU(I  
\255(\261\321\207\361\022SR2K2\363\363\022s \022\005v\031\251E  
\251\352\305\n`\331\334\374”\230\206\002;\000\0225\027\222"

ModelClass.new(:attribute1 =\> compressed\_string).save  
ActiveRecord::StatementInvalid: SQLite3::SQLException: unrecognized  
token: "'x#####+##MU(I#(#Ñ‡####2K2#### v#E####  
################################################

---

<div class="post-metadata">

**Author:** ![pablitoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b487fb/32.png) [@pablitoman](https://www.ruby-forum.com/u/pablitoman)\
**Post date:** [March 26, 2010, 6:52am UTC](https://www.ruby-forum.com/t/saving-gzipped-string-into-sqlite3-via-rails-throws-unrecognized-token-error/185794/2 "2010-03-26T06:52:38Z")

</div>

On Thu, Mar 25, 2010 at 11:30 PM, pablitoman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> should I compress the text in some other fashion?  
> should I save the data using some other method(s)?  
> TIA!!!

I have no idea if this is what is going on because I haven’t worked  
much with SQLite, but are you saving to a TEXT field or a BLOB field?  
If the field is TEXT, try making it a BLOB and see if that works.

-Jonathan N.

---

<div class="post-metadata">

**Author:** ![pablitoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b487fb/32.png) [@pablitoman](https://www.ruby-forum.com/u/pablitoman)\
**Post date:** [March 26, 2010, 7:00am UTC](https://www.ruby-forum.com/t/saving-gzipped-string-into-sqlite3-via-rails-throws-unrecognized-token-error/185794/3 "2010-03-26T07:00:49Z")

</div>

On Mar 26, 1:51Â am, Jonathan N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Any thoughts on what I can do to avoid this error?
> 
> > should I compress the text in some other fashion?  
> > should I save the data using some other method(s)?  
> > TIA!!!
> 
> I have no idea if this is what is going on because I haven’t worked  
> much with SQLite, but are you saving to a TEXT field or a BLOB field?  
> If the field is TEXT, try making it a BLOB and see if that works.
> 
> -Jonathan N.

Nope. didn’t work (thank you, though for the suggestion), but I just  
figured out a “workaround”

if you call “dump” on the zipped string, dump will escape everything  
problematic.

eval will “unescape” and give you back your zipped string

---

<div class="post-metadata">

**Author:** ![pablitoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b487fb/32.png) [@pablitoman](https://www.ruby-forum.com/u/pablitoman)\
**Post date:** [March 26, 2010, 8:40am UTC](https://www.ruby-forum.com/t/saving-gzipped-string-into-sqlite3-via-rails-throws-unrecognized-token-error/185794/4 "2010-03-26T08:40:53Z")

</div>

On Mar 26, 1:51Â am, Jonathan N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Any thoughts on what I can do to avoid this error?
> 
> > should I compress the text in some other fashion?  
> > should I save the data using some other method(s)?  
> > TIA!!!
> 
> I have no idea if this is what is going on because I haven’t worked  
> much with SQLite, but are you saving to a TEXT field or a BLOB field?  
> If the field is TEXT, try making it a BLOB and see if that works.
> 
> -Jonathan N.

Sorry, you were right. I had a bug in my code when I was testing your  
suggestion. converting the columns to blob worked perfectly.

TKS!
