Python的MysqLdb模块是Python连接MysqL的一个模块,默认查询结果返回是tuple类型,只能通过0,1..等索引下标访问数据
默认连接数据库:
查询数据:
cur = conn.cursor()
cur.execute('select b_id from blog limit 1')
data = cur.fetchall()
cur.close()
conn.close()
打印:
for row in data:
print type(row)
print row
执行结果:
<type 'tuple'>
(1L,)
为tuple类型。
我们可以这么干使得数据查询结果返回字典类型,即 字段=数据
导入模块
import MysqLdb.cursors
在连接函数里加上这个参数 cursorclass = MysqLdb.cursors.DictCursor 如:
再重新运行脚本,看看执行结果:
<type 'dict'>
{'b_id': 1L}
搞定!
注意,在连接的时候port如果要指定则值必须是整型,否则会出错!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。