我一直在使用Psycopg2从Postgres中成功读取存储过程并获得一个很好的元组返回,这很容易处理。例如...为什么Psycopg2使用存储过程返回元组列表?
def authenticate(user, password):
conn = psycopg2.connect("dbname=MyDB host=localhost port=5433 user=postgres password=mypwd")
cur = conn.cursor()
retrieved_pwd = None
retrieved_userid = None
retrieved_user = None
retrieved_teamname = None
cur.execute("""
select "email", "password", "userid", "teamname"
from "RegisteredUsers"
where "email" = '%s'
""" % user)
for row in cur:
print row
,打印会给我行('[email protected] ' '84894531656894hashedpassword5161651165',36' 测试“)
然而,当我运行下面的代码用一个存储过程读取一排灯具,我得到(看起来像我)一个邪恶的混乱。
def get_from_sql(userid):
conn = psycopg2.connect("dbname=MyDB host=localhost port=5433 user=postgres password=pwd")
fixture_cursor = conn.cursor()
callproc_params = [userid]
fixture_cursor.execute("select sppresentedfixtures(%s)", callproc_params)
for row in fixture_cursor:
print row
所得输出:
('(5 “2015年8月28日21时○○分00秒”, “2015年8月20日8点00分零零秒”,“2015-08 -25 17:00:00“,”Team“,”Team“,”Final“)',)
我研究了游标类,无法理解它为什么输出这样的存储过程。在Postgres中执行时,输出是一个完美的元组。使用Psycopg2添加到元组,我不明白为什么?
我该如何改变这个,让我得到一个整齐的元组?我对于我提出的这个要求有什么要求?
我已经尝试过callproc函数,并得到一个同样无益的输出。任何想法都会很棒。
你必须发布pl/pgsql函数。对于我们所知道的,你的pl/pgsql函数可以将行转换为一个字符串。 – univerio
CREATE OR REPLACE FUNCTION sppresentedfixtures(useridentity integer) RETURNS SETOF记录AS $ BODY $ 选择“Fixtures”。“Fixture_No”,“Fixtures”。“Fixture_Date”,“Fixtures”。“Opening_Date”,“Fixtures”。 “Home_Side_Score”,“Fixtures”,“Away_Side”,“Predictions”,“Away_Side_Score”,“Fixtures”,“Fixture_Stage” from“Fixtures” left join “预测” 上的 “预测”, “Fixture_No”= “灯具” “Fixture_No” 和 “预测”, “用户ID”=的UserIdentity $ BODY $ 语言SQL VOLATILE COST 100个 ROWS 1000。; ALTER FUNCTION sppresentedfixtures(整数) OWNER TO postgres; – LemusThelroy