8月 232022
使用下面的代码读取mysql数据时出现TypeError: tuple indices must be integers or slices, not str错误
1 2 3 4 5 6 7 8 9 10 11 |
conn = pymysql.connect(host=dbhost, user=dbuser, passwd=dbpasswd, db=dbname) while True: cur = conn.cursor() cur.execute("SELECT * from radardevices where status=1") row_headers = [x[0] for x in cur.description] rows = cur.fetchall() for row in rows: print(row["macaddress"]) cur.close() time.sleep(1) conn.close() |
获取cursor时未指定cursor类型,默认使用tuple类型返回,将cur = conn.cursor()这一行修改为如下就可以了
1 |
cur = conn.cursor(cursor=pymysql.cursors.DictCursor) |
Sorry, the comment form is closed at this time.