[D]psycopg2有没有dictfetchall方法

坚强的豁着 2012-07-03 11:19:00
今天用了一下,提示这个

<type 'exceptions.AttributeError'>: 'psycopg2._psycopg.cursor' object has no attribute 'dictfetchall'
args = ("'psycopg2._psycopg.cursor' object has no attribute 'dictfetchall'",)
message = "'psycopg2._psycopg.cursor' object has no attribute 'dictfetchall'"

在网上找了半天没有这方面的资料,请高手求救

---------------------
Double行动:
原帖分数:40
帖子加分:40
...全文
318 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
坚强的豁着 2012-07-04
  • 打赏
  • 举报
回复
AttributeError: 'module' object has no attribute 'extras'

没理解您的意思
bugs2k 2012-07-03
  • 打赏
  • 举报
回复
psycopg2.extras.DictCursor

[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:

Python code
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchall()
[(1, 100, "abc'def"), (2, None, 'dada'), (3, 42, 'bar')]

Python code

>>> dict_cur = conn.cursor(cursor_f……
[/Quote]
坚强的豁着 2012-07-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Python code
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchall()
[(1, 100, "abc'def"), (2, None, 'dada'), (3, 42, 'bar')]

Python code

>>> dict_cur = conn.cursor(cursor_factory=psycopg2.extr……
[/Quote]
我要结果确实是一个字典,我用

names=[d[0] for d in curs.description]
rows=[dict(zip(names,row)) for row in curs.fetchall()]

代替可否?
bugs2k 2012-07-03
  • 打赏
  • 举报
回复
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchall()
[(1, 100, "abc'def"), (2, None, 'dada'), (3, 42, 'bar')]


>>> dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
>>> dict_cur.execute("INSERT INTO test (num, data) VALUES(%s, %s)",
... (100, "abc'def"))
>>> dict_cur.execute("SELECT * FROM test")
>>> rec = dict_cur.fetchone()
>>> rec['id']
1
>>> rec['num']
100
>>> rec['data']
"abc'def"

37,738

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧