From: Michael Glaesemann Date: 2007-06-28T00:25:37+09:00 Subject: Re: Encoding of strings received from db On Jun 27, 2007, at 9:37 , Marko Marjanovic wrote: > Axel Etzold wrote: >> Dear mare, >> >> does the information here: >> >> http://groups.google.ca/group/rubyonrails-core/browse_thread/ >> thread/db8d0c594cb4cc73 >> >> help ? > > Unfortunately it doesn't, because rails has some kind of configuration > (I was only playing with rails, never digging deeper into it, so > I'm not > sure what it looks like). As for my problem, I'm trying to do this: > > db = PGconn.connect('localhost', 5432, '', '', 'isys', 'postgres', > 'postgres') > res = db.exec('select * from "ADDRESS"') > puts res[1][1] == "S�dstrasse" > > and make the output "true". But i can't, because res[1][1] (second > column of the second row) is in some other encoding and instead of > S�dstrasse ("Sudstrasse" where "u" has umlauts - the two dots above > the > letter) I get S��dstrasse ("u" with umlauts is presented as two > letters). > > I've tried using: > > Iconv.new('UTF-8', 'ISO-8859-1').iconv(res[1][1]) > > but now every byte that I have instead of umlauted u is transformed > into > another two characters - instead of u with umlauts, I now have 4 > characters. > I believe that the problem lies in 'postgres' gem and that it > should be > configured somehow, but I don't have the slightest idea how... I think your issue is that your Ruby script doesn't know what encoding is coming back from the database. Try setting $KCODE = 'u' in your script. $ cat db_encoding_test.rb #!/usr/local/bin/ruby -w $KCODE = 'u' require 'rubygems' require 'postgres' require 'dbi' DBI.connect('dbi:Pg:test:localhost:54824', 'postgres') do |dbh| sth = dbh.prepare(<<-EOS) SELECT * FROM streets; EOS sth.execute sth.fetch do |row| p row p row.first == 'S�dstrasse' end end $ ./db_encoding_test.rb ["S�dstrasse"] true Michael Glaesemann grzm seespotcode net