问题描述
我正在尝试将json对象作为参数传递给存储过程。我尝试了很多事情并最终解决了几个错误,但我陷入了这个错误“ psycopg2连接错误:不能将100个以上的参数传递给函数”,我尝试了json.dumps,它给了我this ..如果我是从具有相同输入的脚本执行可以正常工作。下面是我的存储过程。
CREATE OR REPLACE PROCEDURE public.insert_usr_details_in_all_tables (jsonobject json)
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
skill_s Text[] := '{}';
selectedSkill TEXT;
edu TEXT;
email TEXT;
phone TEXT;
usr_name TEXT;
clg TEXT;
lstemployer TEXT;
gendr TEXT;
skill_row_count INTEGER;
usr_id INTEGER;
skl_id INTEGER;
intEmailExist INTEGER;
expen DECIMAL;
BEGIN
-- Getting name
SELECT jsonobject->'Name' into usr_name;
-- Getting education
SELECT jsonobject->'Education' into edu;
-- Getting phone number
SELECT jsonobject->'phone_number' into phone;
-- Getting experience
SELECT (jsonobject->>'Exp')::DECIMAL INTO expen;
--Getting college
SELECT jsonobject->'College' into clg;
-- Getting last employer
SELECT jsonobject->'CurrentEmployer' into lstemployer;
--Getting gender
SELECT jsonobject->'Gender' into gendr;
-- Getting Email
SELECT json_array_length(jsonobject->'Email') into intEmailExist;
IF intEmailExist > 0 THEN
email:=array(SELECT json_array_elements_text(jsonobject->'Email'));
END IF;
-- Insert user details in 'Extractor_usr_details' table.
INSERT INTO public."Extractor_usr_details"(name,phone_number,email,exp,education,college,"currentEmployer",gender)VALUES ( usr_name,phone,expen,edu,clg,lstemployer,gendr) returning "id" into usr_id;
skill_s := array(SELECT json_array_elements_text(jsonObject->'Skills'));
FOREACH selectedSkill IN ARRAY skill_s LOOP
INSERT INTO public."Extractor_skills" (skill) SELECT selectedSkill WHERE NOT EXISTS (SELECT id FROM public."Extractor_skills" WHERE skill = selectedSkill)RETURNING id into skl_id;
INSERT INTO public."Extractor_usr_skills" (skill_id,user_id) SELECT skl_id,usr_id WHERE NOT EXISTS (SELECT id FROM public."Extractor_usr_skills" WHERE skill_id = skl_id AND user_id = usr_id);
END LOOP;
COMMIT;
END;
$BODY$;
try:
# declare a new Postgresql connection object
conn = connect(
dbname="postgres",user="postgres",host="localhost",password="demo",port= '5432'
# attempt to connect for 3 seconds then raise exception
)
cur = conn.cursor()
cur.callproc('insert_usr_details_in_all_tables',json.dumps(jsonObject))
except (Exception) as err:
print("\npsycopg2 connect error:",err)
conn = None
cur = None
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)