# Inserting string with special character in database

**URL:** <https://www.ruby-forum.com/t/inserting-string-with-special-character-in-database/161070>\
**Category:** Rails\
**Created:** [February 23, 2009, 6:41am UTC](https://www.ruby-forum.com/t/inserting-string-with-special-character-in-database/161070 "2009-02-23T06:41:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anubhaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@anubhaw](https://www.ruby-forum.com/u/anubhaw)\
**Post date:** [February 23, 2009, 6:41am UTC](https://www.ruby-forum.com/t/inserting-string-with-special-character-in-database/161070/1 "2009-02-23T06:41:47Z")

</div>

Hi all.  
I am having trouble in reading string containing special characters from  
file and storing them in database. For example, for string like  
‘\x89\xA5\xCFI w…’  
it throws error “Incorrect string value”. The string should be wrappped  
in duoble quotes, but i didn’t find any method to do so. If any one has  
solution please help. I need it very badly.  
Thanks.

---

<div class="post-metadata">

**Author:** ![anubhaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@anubhaw](https://www.ruby-forum.com/u/anubhaw)\
**Post date:** [February 23, 2009, 9:45am UTC](https://www.ruby-forum.com/t/inserting-string-with-special-character-in-database/161070/2 "2009-02-23T09:45:20Z")

</div>

Quoting Anubhaw P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi all.  
> I am having trouble in reading string containing special characters from  
> file and storing them in database. For example, for string like  
> ‘\x89\xA5\xCFI w…’  
> it throws error “Incorrect string value”. The string should be wrappped  
> in duoble quotes, but i didn’t find any method to do so. If any one has  
> solution please help. I need it very badly.

Which database and what character set, e.g. UTF-8, Latin1, ASCII? Rails  
now  
defaults to UTF-8, but most databases default to Latin1.

Jeffrey

---

<div class="post-metadata">

**Author:** ![anubhaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@anubhaw](https://www.ruby-forum.com/u/anubhaw)\
**Post date:** [February 23, 2009, 10:17am UTC](https://www.ruby-forum.com/t/inserting-string-with-special-character-in-database/161070/3 "2009-02-23T10:17:19Z")

</div>

Jeffrey L. Taylor wrote:

> Quoting Anubhaw P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Hi all.  
> > I am having trouble in reading string containing special characters from  
> > file and storing them in database. For example, for string like  
> > ‘\x89\xA5\xCFI w…’  
> > it throws error “Incorrect string value”. The string should be wrappped  
> > in duoble quotes, but i didn’t find any method to do so. If any one has  
> > solution please help. I need it very badly.
> 
> Which database and what character set, e.g. UTF-8, Latin1, ASCII? Rails  
> now  
> defaults to UTF-8, but most databases default to Latin1.
> 
> Jeffrey

Database is MySql and character set is UTF-8 default.

Anubhaw  
Thanks.

---

<div class="post-metadata">

**Author:** ![anubhaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@anubhaw](https://www.ruby-forum.com/u/anubhaw)\
**Post date:** [February 23, 2009, 11:15am UTC](https://www.ruby-forum.com/t/inserting-string-with-special-character-in-database/161070/4 "2009-02-23T11:15:28Z")

</div>

Anubhaw P. wrote:

> Hi all.  
> I am having trouble in reading string containing special characters from  
> file and storing them in database. For example, for string like  
> ‘\x89\xA5\xCFI w…’  
> it throws error “Incorrect string value”. The string should be wrappped  
> in duoble quotes, but i didn’t find any method to do so. If any one has  
> solution please help. I need it very badly.  
> Thanks.

you can give .to\_s to convert into string.

a= 123  
eg: a.to\_s

---

<div class="post-metadata">

**Author:** ![anubhaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@anubhaw](https://www.ruby-forum.com/u/anubhaw)\
**Post date:** [February 23, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/inserting-string-with-special-character-in-database/161070/5 "2009-02-23T16:24:54Z")

</div>

Quoting Anubhaw P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > solution please help. I need it very badly.  
> > > Thanks.  
> > > The first character is a control code in UTF-8, HTJ. I’ve tried  
> > > assigning and  
> > > saving the above character strings in the Rails console. The saved and  
> > > retrieved string is empty, but I’m not seeing the error “Incorrect  
> > > string  
> > > value”. What is throwing this error? I also don’t understand the part  
> > > about  
> > > wrapping in double quotes. Why can’t you modify your program to change  
> > > single  
> > > to double quotes?

Jeffrey

---

<div class="post-metadata">

**Author:** ![anubhaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@anubhaw](https://www.ruby-forum.com/u/anubhaw)\
**Post date:** [February 24, 2009, 8:36am UTC](https://www.ruby-forum.com/t/inserting-string-with-special-character-in-database/161070/6 "2009-02-24T08:36:33Z")

</div>

Jeffrey L. Taylor wrote:

> Quoting Anubhaw P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > > > solution please help. I need it very badly.  
> > > > Thanks.  
> > > > The first character is a control code in UTF-8, HTJ. I’ve tried  
> > > > assigning and  
> > > > saving the above character strings in the Rails console. The saved and  
> > > > retrieved string is empty, but I’m not seeing the error “Incorrect  
> > > > string  
> > > > value”. What is throwing this error? I also don’t understand the part  
> > > > about  
> > > > wrapping in double quotes. Why can’t you modify your program to change  
> > > > single  
> > > > to double quotes?
> 
> Jeffrey

Hi Jeffrey,  
Your question solved my trouble. I set the database character set to  
Latin1 and it worked. It took the string with special character in DB.

Thanks.
