我想在psycopg2中使用dict光标:

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

这是我的查询:
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 })

但这给了我这个:
<class 'psycopg2.ProgrammingError'> exception: can't adapt type 'DictRow'

有人可以帮忙吗?

最佳答案

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

关于python - psycopg2.ProgrammingError : can't adapt type 'DictRow' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10379440/

10-15 19:46