pymysql中的字典错误
Errors with dictionary in pymysql
我收到以下错误:
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
returns 列表(此处为字典列表 - 因为 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')
我收到以下错误:
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
returns 列表(此处为字典列表 - 因为 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')