用Python写了一个postgresql函数,感觉很爽

来源:互联网 发布:穿小苍淘宝店 编辑:程序博客网 时间:2024/04/29 17:00

用Python写了一个postgresql函数,感觉很爽 

CREATE LANGUAGE plpythonu; 


postgresql函数

CREATE OR REPLACE FUNCTION myfun1(text)
  RETURNS text AS
$BODY$
s = args[0]
h = 0;
n = len(s);
for i, c in enumerate(s):
        h = h + ord(c)*31**(n-1-i);
bits = 4*8;
return (h + 2**(bits-1)) % 2**bits - 2**(bits-1)        


$BODY$
  LANGUAGE 'plpythonu';

调用:返回字符串的hashcode值
  select myfun1('测试'),myfun1('a'),myfun1('A')



上述代码在MAC 笔记本上运行成功

0 0
原创粉丝点击