发布网友 发布时间:2022-04-09 17:23
共2个回答
懂视网 时间:2022-04-09 21:44
MySQLdb import MySQLdb.cursors conn = MySQLdb.Connect ( host = ‘localhost‘, user = ‘root‘ , passwd = ‘‘, db = ‘test‘, compress = 1, cursorclass = MySQLdb.cursors.DictCursor, charset=‘utf8‘) // <- important cursor = conn.cursor() cursor.execute ("SELECT name, txt FROM table") rows = cursor.fetchall() cursor.close() conn.close()
# another (even better) way is:
conn = MySQLdb . Connect ( host = ‘ localhost ‘, user = ‘root‘ , passwd = ‘‘, db = ‘test‘ , compress = 1) cursor = conn.cursor (cursorclass = MySQLdb.cursors.DictCursor) # ... # results by field name cursor = conn.cursor() # ... # ...results by field number
注意:如果同时安装了PyMySQL和MySQL-python库,会导致MySQL-python失效,只有pymsql可用
【mysql】MySQLdb返回字典方法
标签:row etc ber 字典 table number test 维护 com
热心网友 时间:2022-04-09 18:52
Python的MySQLdb模块是Python连接MySQL的一个模块,默认查询结果返回是tuple类型,只能通过0,1..等索引下标访问数据默认连接数据库: