gpt4 book ai didi

python - pymysql 中的字典错误

转载 作者:行者123 更新时间:2023-11-29 12:26:09 26 4
gpt4 key购买 nike

我收到以下错误:

if 'ShazK01' in LogRef.values():
AttributeError: 'list' object has no attribute 'values'

运行后:

import pymysql 

db = pymysql.connect(host='localhost', ..)
cur = db.cursor()

def getData():
LoginInput=[]
InputUN = ('ShazK01')
InputPC = ('passinput')
LoginInput=[InputUN, InputPC]
return LoginInput


def checkExistence():
cur = db.cursor(pymysql.cursors.DictCursor)
cur.execute("SELECT Username FROM LoginDetails")
LogRef = cur.fetchall() #fetch ALL login references
print(LogRef)
if 'ShazK01' in LogRef.values():
print("yes")
else:
print("no")

LoginInput = getData()
print(LoginInput)
print(LoginInput[0])
UNInput = LoginInput[0]
print(UNInput)
checkExistence()

我看过其他答案,但没有帮助。因此,任何有用的建议都将受到赞赏,因为我是同时使用 sql 和 python 的初学者,谢谢!

最佳答案

PyMysql 的 fetchall 返回一个列表(这里是字典列表 - 因为 pymysql.cursors.DictCursor)。你必须写一些类似的东西

from itertools import chain
it = chain.from_iterable(x.values() for x in cur.fetchall())
print('yes' if 'ShazK01' in it else 'no')

print('yes' if any('ShazK01' in x.values() for x in cur.fetchall()) else 'no')

关于python - pymysql 中的字典错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28285363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com