3 回答
TA贡献1797条经验 获得超6个赞
你不能。按照设计,SQL 返回元组。在 python 中访问您的列表值,如下所示:
cursor.execute("SELECT password FROM [dbo].[passes] where user_name ='test'")
for row in cursor.fetchall():
print (row)
passw = input("enter pass ")
if passw == row['password']: # Could also be row[0], idk you have to try
print ("nice")
else:
print ("wrong")
TA贡献1789条经验 获得超10个赞
row[0] 建议有效,只是略有不同:
cursor.execute("SELECT RTRIM (password) FROM [dbo].[passes] where user_name = 'test'")
for row in cursor.fetchall():
print (row)
sqlpass = row[0]
print (sqlpass)
passw = input("enter pass ")
if passw == sqlpass:
print ("nice")
else:
print ("wrong")
RTRIM 是必需的,因为该字段是一个 varchar(20) 并且返回的字符串是存储的字符串加上任意数量的空格以获得 20 个字符。
TA贡献1804条经验 获得超8个赞
在这种情况下,如何从数据库中仅检索值 5555,而不是列表格式?
pyodbc 支持返回单个标量值的.fetchval()方法:
count = cursor.execute('select count(*) from users').fetchval()
添加回答
举报