使用python psycopg

时间:2018-08-09 21:59:30

标签: python sql procedures

我正在尝试使用SQL创建SQL程序spAppeUpdatetTimes(),该SQL接受输入中UPC项的字符串参数。但是它不能正常工作。在输出中,该过程采用的输入不是实心字符串,而是几个参数。这是因为该项目的UPC值被拆分(划分为)单个字符。我应该怎么做才能避免这个错误?

CREATE OR REPLACE FUNCTION external.spAppeUpdatetTimes(
upc character varying)

  RETURNS void
  LANGUAGE 'sql'

  COST 100
  VOLATILE 
AS $BODY$

    update external."tbProducts" as pu
            set "eBayDiscontinued"= current_timestamp
    from external."tbProducts" as ps 
    where pu."eBayUPC"=upc;
    end;

  $BODY$;

  ALTER FUNCTION external.spappeupdatettimes(character varying)
       OWNER TO postgreadmin;

在Python中调用该过程:

 cursor.callproc('external."spAppeUpdatetTimes"', (record[2]))

包含数据的表:

CREATE TABLE external."tbProducts"
("UPC" character varying(255) COLLATE pg_catalog."default",
    CONSTRAINT "tbProducts_pkey" PRIMARY KEY ("CWRPartNumber"))

但是,当我尝试使用该功能时,出现以下错误:

ProgrammingError('function external.spAppeUpdatetTimes(unknown, 
unknown, unknown, unknown, unknown, unknown) does not exist\nLINE 1: 
SELECT * FROM external."spAppeUpdatetTimes" 
 (\'1\',\'9\',\'3\',\'0\',...\n                      ^\nHINT:  No 
  function matches the given name and argument types. You might need 
  to add explicit type casts.

2 个答案:

答案 0 :(得分:0)

尝试显式加入数据。

类似

cursor.callproc('external."spAppeUpdatetTimes"', ''.join((record[2])))

答案 1 :(得分:0)

问题已解决。使用python代码进行过程调用时,参数'callproc'必须为元组或列表。 mysqltutorial.org/calling-mysql-stored-procedures-python。

cursor.callproc('external."spAppeUpdatetTimes"', (record[2], ))