pymysql中的字典错误

时间:2015-02-02 19:35:38

标签: python mysql dictionary 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的初学者,谢谢!

1 个答案:

答案 0 :(得分:1)

PyMysql' s 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')