【发布时间】:2018-03-12 14:52:26
【问题描述】:
我在使用 mySQL 时遇到了这个问题。我的结果 = [{'email': 'bernie@shrimp.com'}] 但是当我在做 email = results[0]['email'] 时我得到一个 TypeError: tuple indices must be integers or slices, not str
问题是当我在本地运行它时,它运行良好。如何获得bernie@shrimp.com?
users 是一张表
代码:
cursor.execute('SELECT email FROM users WHERE username = %s', [attempted_username])
email_dict = cursor.fetchall()
print(email_dict)
session['email'] = email_dict[0]['email']
控制台:
[{'email': 'bernie@shrimp.com'}]
【问题讨论】:
-
由于您的变量名称不匹配,您可以使用正确的代码编辑问题。比如我的结果和结果。
-
我在上面添加了一些 - 告诉我是否需要更多
-
无论有没有会话,问题都会出现
-
您的控制台输出令人困惑,您可以检查一下它的 [{}] 还是 [()]
-
我复制并粘贴了@Harry
标签: python mysql pythonanywhere