关于MySQLdb

test_test81 2010-01-20 02:23:17
import MySQLdb
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '?????', db = 'test')
cursor = db.cursor()
cursor.execute('select * from cb_table')
rs = cursor.fetchall()
print rs

"""
如果我把
cursor = db.cursor()
cursor.execute('select * from cb_table')
改为
db.cursor().execute('select * from callback_table')
就会报ProgrammingError: execute() first错误为什么呢
"""
...全文
114 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
mrshelly 2010-01-21
  • 打赏
  • 举报
回复
因为第一种, 会生成一个范围内有效的实例 cursor 这个实例在后面的 fetchall 中用到.

而第二种, 则只会在.execute 行产生一个有效范围只在一行内的内建 cursor 实例.
而这个实例你没有办法继续使用.

如果第二种方式.
你 db.cursor().execute(...)
再fetchall 时,你用啥呢?
db.cursor().fetchall...?

你确认 两个db.cursor()是同一个实例吗? 如果你的应用不要求在 cursor 中再继续,如使用 update insert 等语句.只要求 execute 即可的话, 是可以那样写的.

37,743

社区成员

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

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