我有一个简单的程序来检查一个电子邮件和散列密码是否在Postgres数据库中(它本来是一个cgi应用程序,但由于它失败了,我首先将它作为脚本进行测试):
import cgi
import cgitb
import hashlib
import psycopg2
from dbconfig import *
cgitb.enable()
print "Content-Type: text/plain\n\n";
def checkPass():
email = "person@gmail.com"
password = "mypassword"
password = hashlib.sha224(password.encode()).hexdigest()
result = cursor.execute('SELECT * FROM logins WHERE email=%s AND passhash=%s', (email, password))
print result
if __name__ == "__main__":
conn_string = "host=%s dbname=%s user=%s password=%s" % (host, database, dbuser, dbpassword)
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
checkPass()此程序在打印时始终不返回任何内容。不过,电子邮件和散列密码肯定在数据库中。我把它们和这个放在一起:
email = "person@gmail.com"
allpass = "mypassword"
password = hashlib.sha224(allpass.encode()).hexdigest()
cursor.execute('INSERT INTO logins (email, passhash) VALUES (%s, %s);', (email, password))
conn.commit()检查数据库显示有电子邮件和散列密码。那么,为什么第一个程序不返回SELECT语句呢?散列不是以相同的方式存储的吗?任何帮助都将不胜感激。
发布于 2017-04-18 17:33:34
我想出了什么问题。碰巧,cursor.execute()实际上没有返回任何内容。为此,您必须使用fetchone()。
cursor.execute('SELECT * FROM logins WHERE email=%s AND passhash=%s;', (email, password))
result = cursor.fetchone()
print result这将打印正确结果的元组。
https://stackoverflow.com/questions/43476331
复制相似问题