通过 Psycopg 插入到 Pg
如何修复 Python 中的 SQL 语句?
数据库连接有效。但是,cur.execute
返回 none,这是错误的。
我的代码
import os, pg, sys, re, psycopg2
try:
conn = psycopg2.connect("dbname='tk' host='localhost' port='5432' user='naa' password='123'")
except: print "unable to connect to db"
cur = conn.cursor()
print cur.execute("SELECT * FROM courses") # problem here
Psql 中的 SQL 命令返回正确的输出。
我可以类似地在 Psql 中运行 INSERT
,但不能通过 Python 脚本运行。
我没有收到 /var/log 的警告/错误。
可能的错误是
- cursor(),似乎是正确的
- 方法的语法connect(),但是似乎没问题
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须调用
cur
上的fetch
方法之一(fetchone、fetchmany、fetchall)才能实际获取查询结果。您可能应该阅读 教程 for DB-API。
You have to call one of the
fetch
methods oncur
(fetchone, fetchmany, fetchall) to actually get the results of the query.You should probably have a read through the a tutorial for DB-API.
您必须调用 cur.fetchall() 方法(或其他 fetch*() 方法之一)才能从查询中获取结果。
You have to call
cur.fetchall()
method (or one of otherfetch*()
methods) to get results from query.