# Ruby chinese character encoding problem with SQL

**URL:** <https://www.ruby-forum.com/t/ruby-chinese-character-encoding-problem-with-sql/106788>\
**Category:** Ruby\
**Created:** [June 14, 2007, 3:45am UTC](https://www.ruby-forum.com/t/ruby-chinese-character-encoding-problem-with-sql/106788 "2007-06-14T03:45:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [June 14, 2007, 3:45am UTC](https://www.ruby-forum.com/t/ruby-chinese-character-encoding-problem-with-sql/106788/1 "2007-06-14T03:45:32Z")

</div>

Hi,

My SQL query has some chinese characters which are getting messed up.  
The SQL server uses gb2312(chinese) encoding and so in my Ruby script I  
use Iconv to convert from UTF-8 to gb2312. The conversion seems to work  
fine, I have tried dumping the output to a text file and it all looks  
great, but when it’s sent to the SQL server the chinese character get  
messed up(which I got to know after looking at the trace log). I’m using  
the DBI module to connect to the SQL server and “execute” function to  
send the query. Can anyone please suggest a fix ?

Thanks,  
Bob

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [June 14, 2007, 9:25am UTC](https://www.ruby-forum.com/t/ruby-chinese-character-encoding-problem-with-sql/106788/2 "2007-06-14T09:25:41Z")

</div>

Dear Bob,

maybe you can use Mysql.escape (see the “Including Special Characters  
in Statement Strings” of  
[http://www.kitebird.com/articles/ruby-mysql.html](http://www.kitebird.com/articles/ruby-mysql.html)  
to produce proper output.  
I don’t know any Chinese myself, but I had a similar problem with  
European accents in web page addresses recently. The most elegant  
solution was to use CGI.escape(string), so maybe something similar  
will work for you using MySql.

Best regards,

Axel
