Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Interfaces jdbc

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-16-2008, 01:00 AM
antony baxter
 
Posts: n/a
Default Character Encoding problem

Hi,

I'm having a character set problem, and I wonder if anyone here could
sanity check what I'm doing. It might well be that the problem lies
elsewhere.

My database was created with -E UNICODE, and when I do a \l in psql it
is listed as UTF8.

My Java application is receiving data over a socket which is encoded
in UTF8. I'm logging this and it is displaying e.g. Cyrillic or Greek
correctly (using OSX Terminal.app which supports UTF8, tailing the log
with 'less' and the environment variable LESSCHARSET=utf-8.

I'm persisting this data using the latest 8.3 JDBC drivers into
PostgreSQL 8.3.0. I'm not changing the client_encoding (I tried, but I
understand that the JDBC drivers set it to UNICODE anyway, and throw
an error if I attempt to change it to anything else). The data writes
fine, and if I then do a SELECT and a resultSet.getString(x) and write
the output to the log, everything still looks fine. I'm therefore
satisfied that Java + JDBC drivers + PostgreSQL are able to write &
read the data fine. So far so good.

However, if using psql I try to look at the data, it is mangled. If I
try a manual UPDATE via psql using the data cut'n'pasted from my log,
and then look at the data, it reads correctly. Therefore I know that
psql is capable of reading and writing UTF8 data correctly. Also, the
client application that reads from my database is Perl, and this also
retrieves mangled data; we've tried writing and reading directly from
Perl, and in this case reviewing the data in psql looks normal.

The conclusion I've reached is that Java + JDBC is not actually
persisting the data in UTF-8; is that correct or am I wildly off base,
and if it is correct then is there anything I can do about it?!

Many thanks,

Ant.

--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-16-2008, 01:00 AM
antony baxter
 
Posts: n/a
Default Re: Character Encoding problem

One thing I forgot to add; I also tried e.g.:

ps.setString(1, new
String(Charset.forName("UTF-8").encode(myString).array(), "UTF-8"));

to be absolutely certain that I was passing UTF-8 to the database; this threw a

22047 [Thread-2] DEBUG com.test.database.postgresql.Dao - PSQL
Exception State: 22021
22047 [Thread-2] DEBUG com.test.database.postgresql.Dao - PSQL
Exception Message: invalid byte sequence for encoding "UTF8": 0x00
22051 [Thread-2] ERROR com.test.database.postgresql.Dao - Error Storing Data:
org.postgresql.util.PSQLException: ERROR: invalid byte sequence for
encoding "UTF8": 0x00
at org.postgresql.core.v3.QueryExecutorImpl.receiveEr rorResponse(QueryExecutorImpl.java:1592)
at org.postgresql.core.v3.QueryExecutorImpl.processRe sults(QueryExecutorImpl.java:1327)
at org.postgresql.core.v3.QueryExecutorImpl.execute(Q ueryExecutorImpl.java:192)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execut e(AbstractJdbc2Statement.java:451)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execut eWithFlags(AbstractJdbc2Statement.java:350)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execut e(AbstractJdbc2Statement.java:343)
at com.test.database.postgresql.Dao.store(Dao.java:19 7)
...

I presume that this is because the JDBC driver is expecting the JVM's
internal UTF16 String representation?

Ant



On Mon, Apr 7, 2008 at 8:29 AM, antony baxter <antony.baxter@gmail.com> wrote:
> Hi,
>
> I'm having a character set problem, and I wonder if anyone here could
> sanity check what I'm doing. It might well be that the problem lies
> elsewhere.
>
> My database was created with -E UNICODE, and when I do a \l in psql it
> is listed as UTF8.
>
> My Java application is receiving data over a socket which is encoded
> in UTF8. I'm logging this and it is displaying e.g. Cyrillic or Greek
> correctly (using OSX Terminal.app which supports UTF8, tailing the log
> with 'less' and the environment variable LESSCHARSET=utf-8.
>
> I'm persisting this data using the latest 8.3 JDBC drivers into
> PostgreSQL 8.3.0. I'm not changing the client_encoding (I tried, but I
> understand that the JDBC drivers set it to UNICODE anyway, and throw
> an error if I attempt to change it to anything else). The data writes
> fine, and if I then do a SELECT and a resultSet.getString(x) and write
> the output to the log, everything still looks fine. I'm therefore
> satisfied that Java + JDBC drivers + PostgreSQL are able to write &
> read the data fine. So far so good.
>
> However, if using psql I try to look at the data, it is mangled. If I
> try a manual UPDATE via psql using the data cut'n'pasted from my log,
> and then look at the data, it reads correctly. Therefore I know that
> psql is capable of reading and writing UTF8 data correctly. Also, the
> client application that reads from my database is Perl, and this also
> retrieves mangled data; we've tried writing and reading directly from
> Perl, and in this case reviewing the data in psql looks normal.
>
> The conclusion I've reached is that Java + JDBC is not actually
> persisting the data in UTF-8; is that correct or am I wildly off base,
> and if it is correct then is there anything I can do about it?!
>
> Many thanks,
>
> Ant.
>


--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-16-2008, 01:00 AM
Craig Ringer
 
Posts: n/a
Default Re: Character Encoding problem

antony baxter wrote:
> However, if using psql I try to look at the data, it is mangled.

What's your terminal encoding? The easiest way to find out is to look at
the output of the `locale' command.

What is the output of the psql '\encoding' command when you've just made
a new connection, and without touching the client_encoding setting?

If your terminal encoding is not utf-8 then setting client_encoding to
utf-8 in psql is incorrect. You should set it to your local 8 bit
encoding (say, latin-1) so psql knows to convert text between the DB
encoding and local encoding.

I'm a little surprised that psql doesn't set client_encoding by default
based on the LC_ALL / LANG environment variables, but it doesn't seem
to. I'm in a UTF-8 locale (en_AU.UTF-8) and even if I run psql as:

LANG=en_AU LC_ALL=en_AU psql

(en_AU is a LATIN-1 locale by default) it still selects a UTF-8 client
encoding. If I spawn a terminal in the en_AU locale and do this I get
mangled text from psql until I set client_encoding manually.

Shouldn't psql set client_encoding based on the user's locale?
> If I
> try a manual UPDATE via psql using the data cut'n'pasted from my log,
> and then look at the data, it reads correctly.

That's not necessarily correct. If your terminal encoding is not UTF-8
then `less' is converting the UTF-8 encoded data in the log to your
locale encoding. Copying and pasting that from a terminal displaying it
with less will copy the /translated/ data, not the original UTF-8 data.

To perform a valid test you'd need to extract the raw byte sequence from
the log, encode it as an escaped octal/hex string in psql, and send that.
> Therefore I know that
> psql is capable of reading and writing UTF8 data correctly.

It is, but I'm not sure that your tests show that. I think you're
silently inserting data in a different (non-UTF-8) encoding into the
database.

If you can compare the byte sequences of the value when you've just set
it from Java to when you've just set it from psql I could tell you for
sure. Casting the text to a bytea should do the job, eg running:

SELECT problem_column::bytea FROM ...

after you insert/update from java, and again after you insert/update
from psql.


Alternately, just insert the UTF-8 string you want as a bytea. Here's an
example string for you (UTF-8 encoded in my mail client):

SELECT 'äëïöüáé*óú©讓您搜尋和瀏覽最新 資訊。'::bytea;

the string is aeiou (all with umlaut), aeiou (all with acute), copyright
symbol, then a bunch of simplified chinese characters from google news.
If it doesn't appear that way in your mail client then it's not
honouring the content encoding set in the message, and you need to
manually tell it to handle the message as utf-8.

The resulting string of UTF-8 encoded bytes is:

E'\303\244\303\253\303\257\303\266\303\274\303\241 \303\251\303\255\303\263\303\272\302\251\350\256\2 23\346\202\250\346\220\234\345\260\213\345\222\214 \347\200\217\350\246\275\346\234\200\346\226\260\3 50\263\207\350\250\212\343\200\202'

If you INSERT that string into a text/varchar column in the DB as an
escaped string as above, then SELECT it back as bytea, you should get
the original sequence of octal values back. If you select it as text,
you should get the same value as the string shown above in my email. If
you insert the non-escaped string as written in my email and select that
back out as bytea, you should get the same sequence of octal values as
shown in my mail.
> Also, the
> client application that reads from my database is Perl, and this also
> retrieves mangled data; we've tried writing and reading directly from
> Perl, and in this case reviewing the data in psql looks normal.
>

What locale is your perl app running in?

Are you explicitly handling conversions from utf-8 byte strings from
psql to the local 8 bit encoding, or setting client_encoding to your
local 8 bit encoding to get the pg libraries to do it for you?
> The conclusion I've reached is that Java + JDBC is not actually
> persisting the data in UTF-8; is that correct or am I wildly off base,
> and if it is correct then is there anything I can do about it?!
>

It's actually rather a lot more likely that the rest of your tests are
inserting mangled data and then retrieving it by unmangling it the same
way, and the Java code is getting it right. I see this sort of thing a
lot when people are working with 8-bit text of different encodings, or
converting between "Unicode" datatypes and 8-bit strings that don't have
an inherent encoding.

--
Craig Ringer

--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-16-2008, 01:00 AM
Craig Ringer
 
Posts: n/a
Default Re: Character Encoding problem

antony baxter wrote:
> One thing I forgot to add; I also tried e.g.:
>
> ps.setString(1, new
> String(Charset.forName("UTF-8").encode(myString).array(), "UTF-8"));
>

That should just be a no-op in encoding terms. Could the ByteBuffer
returned by encode(...) contain trailing NULL values that CharsetDecoder
retains in the decoded string?

Are the lengths of `myString' and the converted string the same?

If you print them out character by character what're the character values?

--
Craig Ringer

--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-16-2008, 01:00 AM
Craig Ringer
 
Posts: n/a
Default Re: Character Encoding problem

antony baxter wrote:

> Displaying 'input' character by character:
> Character 0 = '8211'
> Character 1 = '235'
> Character 2 = '8212'
> Character 3 = '196'
> Character 4 = '8212'
> Character 5 = '231'
> Character 6 = '8211'
> Character 7 = '937'
> Character 8 = '8212'
> Character 9 = '199'


There's your problem. Your *input* is mangled.

The above decodes to:

--e"---A"---c,--?---C,

So at some point you or some library you're using has done something
like read a utf-8 byte sequence from a file and shoved it character by
character into a String. Another possible culprit is a wrong (implicit?)
encoding conversion or cast from a byte array type to a unicode string type.

The JDBC is storing exactly what you tell it to, and the good 'ol GIGO
rule is being applied.

--
Craig Ringer

--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-16-2008, 01:00 AM
Craig Ringer
 
Posts: n/a
Default Re: Character Encoding problem

antony baxter wrote:
> Hi Craig - thanks for replying.
>
>> What's your terminal encoding? The easiest way to find out is to look at
>> the output of the `locale' command.

>
> ant@home (/Users/ant) % locale
> LANG="en_GB.UTF-8"


Hmm, ok. I was expecting a non-utf-8 locale.

>
>> What is the output of the psql '\encoding' command when you've just made a
>> new connection, and without touching the client_encoding setting?

>
> testdb=# \encoding
> UTF8


OK, so your psql client encoding matches your db locale and your local 8
bit text codec for your terminal etc.

>> To perform a valid test you'd need to extract the raw byte sequence from
>> the log, encode it as an escaped octal/hex string in psql, and send that.

>
> Ok. I tried cutting and pasting just some cyrillic from my log file
> into 'test.txt' and then did:
>
> ant@home (/Users/ant) % file test.txt
> test.txt: UTF-8 Unicode text
>
> Which I assumed was enough?


Only because all your locales are the same. Copy and paste via a
terminal is potentially subject to text encoding conversions by tools
like `less' and `vim'.

A reliable test would be to use something like Perl or Python to open
the file (in plain old 8-bit binary mode, no encoding conversions etc),
seek to the appropriate part, and read the sequence of bytes of interest.

> Ok. Directly after the Java INSERT:
>
> testdb=# select name::bytea from testTable where id = '1';
> first_name
> ------------------------------------------------------------------------------------------------------
> \342\200\223\303\253\342\200\224\303\204\342\200\2 24\303\247\342\200\223\316\251\342\200\224\303\207
> (1 row)


That decodes as:

–ë—Ä—ç–Ω—Ç

ie emdash, e-umlaut, emdash, captial-a-umlaut, emdash, c-cedelia,
emdash, omega, emdash, capital-c-cedelia.

.... which isn't exactly cyrillic ;-)

More to the point, the regularly inserted emdashes are very informative,
as they suggest that something is mangling the UTF-8 escape bytes.

> Then I manually UPDATE and try again:
>
> testdb=# select name::bytea from testTable where id = '1';
> first_name
> ------------------------------------------
> \320\221\321\200\321\215\320\275\321\202


.... which decodes as a much more sensible 'Брэнт'. At least it's cyrillic.


>> If you INSERT that string into a text/varchar column in the DB as an
>> escaped string as above, then SELECT it back as bytea, you should get the
>> original sequence of octal values back. If you select it as text, you should
>> get the same value as the string shown above in my email. If you insert the
>> non-escaped string as written in my email and select that back out as bytea,
>> you should get the same sequence of octal values as shown in my mail.

>
> Both appear correct - i.e. SELECT name shows me correctly accented +
> Chinese text, and SELECT name::bytea shows me the same bytes.


Yep, it's now clear that you're getting the right behaviour from psql.
Probably perl, too, since it's the same 8-bit encoding all the way through.

>> Are you explicitly handling conversions from utf-8 byte strings from psql
>> to the local 8 bit encoding, or setting client_encoding to your local 8 bit
>> encoding to get the pg libraries to do it for you?

>
> Neither, as far as I'm aware. I'm running everything (I think) in UTF-8.


It's probably a good idea to set the client encoding explicitly to your
local encoding anyway, or do explicit utf-8 <> local8bit conversions on
data from the DB. Testing your perl code in a non-utf-8 locale

>> It's actually rather a lot more likely that the rest of your tests are
>> inserting mangled data and then retrieving it by unmangling it the same way,
>> and the Java code is getting it right. I see this sort of thing a lot when
>> people are working with 8-bit text of different encodings, or converting
>> between "Unicode" datatypes and 8-bit strings that don't have an inherent
>> encoding.

>
> Yes - I think thats probably right, except that everything else
> *seems* to be functioning correctly...


Yep, my bad. So now we look at your strings in Java. Are you certain
that the input you're feeding to the JDBC is correctly encoded utf-8
text? I've frequently seen a "unicode" string like java.lang.String or
Qt's QString being filled character by character from 8-bit input,
resulting in a hopelessly mangled string for non-ascii input, especially
in locales where one character isn't necessarily one byte. Treating
utf-8 input as ascii or latin-1 (maybe due to an implicit conversion
somewhere) is another culprit.

--
Craig Ringer


--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-16-2008, 01:00 AM
Craig Ringer
 
Posts: n/a
Default Re: Character Encoding problem

Craig Ringer wrote:

> The above decodes to:
>
> --e"---A"---c,--?---C,


Argh, mail clients are evil.

Manually setting the encoding to utf-8 again, the output decodes to:

–ë—Ä—ç–Ω—Ç

Sorry.

The rest of the mail stands.

--
Craig Ringer

--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:21 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881