PostgreSQL , 字符集 , 汉字编码 , 随机汉字 , chr , ascii , unicode
4e00 对应 19968
9fa5 对应 40869
一共 20901 个汉字
例子
set standard_conforming_strings =off; set escape_string_warning=off; postgres=# select '\u9fa5'::text; text ------ 龥 (1 row)除了unicode的写法,PostgreSQL还提供了两个函数,支持数值的写法。
FunctionReturn TypeDescriptionExampleResultascii(string)intASCII code of the first character of the argument. For UTF8 returns the Unicode code point of the character. For other multibyte encodings, the argument must be an ASCII character.ascii('x')120chr(int)textCharacter with the given code. For UTF8 the argument is treated as a Unicode code point. For other multibyte encodings the argument must designate an ASCII character. The NULL (0) character is not allowed because text data types cannot store such bytes.chr(65)A postgres=# select chr(19968); chr ----- 一 (1 row)例子
do language plpgsql $$ declare res text := ''; begin for i in 19968..40869 loop res := res||chr(i); end loop; raise notice '%', res; end; $$; NOTICE: 一丁丂七丄丅丆万丈三上下丌不与丏丐丑丒专且丕世丗丘丙业丛东丝.............................例子
创建函数
create or replace function gen_hanzi(int) returns text as $$ declare res text; begin if $1 >=1 then select string_agg(chr(19968+(random()*20901)::int), '') into res from generate_series(1,$1); return res; end if; return null; end; $$ language plpgsql strict;使用函数,生成随机汉字
postgres=# select gen_hanzi(10); gen_hanzi ---------------------- 析埲錀噝穎灯嬪閸醿厧 (1 row) postgres=# select gen_hanzi(10); gen_hanzi ---------------------- 仫哸擡襖批梹繜嚪隶別 (1 row)http://baike.baidu.com/item/Unicode
http://unicode.org/charts/
https://en.wikipedia.org/wiki/Unicode
《PostgreSQL 转义、UNICODE、与SQL注入》
http://stackoverflow.com/questions/3970795/how-do-you-create-a-random-string-thats-suitable-for-a-session-id-in-postgresql
https://www.postgresql.org/docs/9.6/static/functions-string.html