开发者

SQL select with Unicode

I have database with encoding UTF-8, collation and ctype ru_RU.UTF-8. Table structure - id, key, value. One line inserted:

1 | size | --- "\xD0\xA0\xD0\xB0\xD0\xB7\xD0\xBC\xD0\xB5\xD1\x80"|

When I executed

SELECT E'--- "\xD0\xA0\xD0\xB0\xD0\xB7\xD0\xBC\xD0\xB5\xD1\x80"';

in console, I got the correct output

   ?column?   
   --------------
    --- "Размер"
   (1 row)

But when I tried to execute

select "value" from "translations" where "key"='size';

I got

   value                          
   -----------开发者_JAVA百科---------------------------------------------
    --- "\xD0\xA0\xD0\xB0\xD0\xB7\xD0\xBC\xD0\xB5\xD1\x80"
   (1 row)

How do I get the unescaped string?

CREATE TABLE translations (
    id serial NOT NULL,
    "key" character varying(255) NOT NULL,
    "value" text,
    CONSTRAINT translations_pkey PRIMARY KEY (id) )
 WITH ( OIDS=FALSE );
 ALTER TABLE translations OWNER TO user;
 CREATE UNIQUE INDEX index_translations_on_key ON translations USING btree (key);


CREATE OR REPLACE FUNCTION eval(text) RETURNS text AS
$BODY$  
DECLARE
  s text;
  r text;
BEGIN
  s := 'select ' || $1 ;
  execute s into r;
  return r;
END;
$BODY$ LANGUAGE 'plpgsql' VOLATILE;

select eval(E'E\'' || "value" || E'\'') from translations where key = 'size' and "value" IS NOT NULL;

looks awful, but works :)


I don't understand why you store escaped values in the database, but as far as I can tell, the decode function should do what you want.

SELECT decode(value, 'hex')
FROM translations;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜