postgresql psycopg2.ProgrammingError:无法适应类型“DictRow”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10379440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:52:32  来源:igfitidea点击:

psycopg2.ProgrammingError: can't adapt type 'DictRow'

pythonpostgresqlpsycopg2

提问by Majid Azimi

I want to use dict cursor in psycopg2:

我想在以下位置使用 dict 游标psycopg2

self.__db_conn = psycopg2.extras.DictConnection("host=... dbname=...")

here is my query:

这是我的查询:

cur.execute('INSERT INTO scm_main.tbl_ack(ack_summary_id, ack_local_timestamp, ack_user_id) '
    'SELECT summary_id AS ack_summary_id, now() AS ack_local_timestamp, us.user_id AS ack_user_id '
    'FROM scm_main.tbl_summary AS s '
    'INNER JOIN scm_main.vu_usr_stn AS us ON (s.summary_station_id = us.station_axis_id) '
    'WHERE ((s.summary_id > (SELECT COALESCE(max(a.ack_summary_id),0) FROM scm_main.tbl_ack AS a WHERE a.ack_user_id = %(user_id)s)) '
    'AND (s.summary_company_specific_id <> 0) '
    'AND (us.user_name = %(user_name)s) AND (s.summary_timestamp < (now() - \'00:25:00\'::interval))) '
    'ORDER BY s.summary_id ASC', { 'user_id': self.__user_id, 'user_name': self.__company })

But it gives me this:

但它给了我这个:

<class 'psycopg2.ProgrammingError'> exception: can't adapt type 'DictRow'

can anyone help?

有人可以帮忙吗?

采纳答案by fog

Complex types, like DictRow(that is a full results row, indexable by column name) can't be automatically adapted to simple SQL types and psycopg is just telling you that. Your code seems fine so the error almost surely is that you wanted to put the result of a query in the self.__user_idand self.__companyattributes but ended up putting the whole result set, i.e., the DictRow, in one or both of them. Check the code that fetches the results.

复杂类型,例如DictRow(即完整的结果行,可按列名索引)不能自动适应简单的 SQL 类型,而 psycopg 只是告诉您这一点。您的代码看起来不错,因此错误几乎肯定是您想将查询结果放入self.__user_idself.__company属性中,但最终将整个结果集(即 )DictRow放入其中一个或两个中。检查获取结果的代码。