使用Python,Phpmyadmin登录身份验证,"SELECT"问题


I've a problem in this SELECT:

前提条件:

我做了一个脚本,用一个"密钥"创建一个数据库,这个"密钥"是一个加密的密码->加密代码:

import base64
import os
from cryptography.hazmat.backends import  default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
password_provided = "password"  # input
password = password_provided.encode()   # Convert in type byte
salt = b'xaesxffx80xe2| (xfcGxbdkxedxb9x15n7'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(password)) #can only use kdf once
print(key)

现在我正在尝试用POST方法进行AUTH,我开始尝试用SELECT来"搜索"日志的密码,并找到一种方法来比较未加密的字符串和加密的登录字符串。我尝试:

mycursor = mydb.cursor()
sql = "SELECT password FROM users WHERE password = %s", (key) #Problem!
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)

我的输出是:

**  File "login.py", line 12, in <module>
mycursor.execute(sql)
File "/Users/jhon/prova/lib/python3.7/site-packages/mysql/connector/cursor.py", line 536, in execute
stmt = operation.encode(self._connection.python_charset)
AttributeError: 'tuple' object has no attribute 'encode'
**

我试着做"key1=key.encode((",但它错了。。。谢谢大家。

此行创建一个字符串和一个int的2元组,并将其赋给变量"sql":

sql="从用户中选择密码WHERE password=%s";,(key(#问题!

"(键(";不产生1-元组;做你想做的事;(键(";。看起来您想要创建一个字符串的2元组,后跟一个1元组。

然后这一行将您创建的2元组作为单个参数发送到被调用的方法:

mycursor.execute(sql(

如果这是您正在使用的库,则函数定义的docstring在此处执行(请参见第183行(…:https://github.com/mysql/mysql-connector-python/blob/master/lib/mysql/connector/cursor.py

显示您希望使用一个n元组作为.execute(…(的第二个函数参数来指定查询模式填充值https://medium.com/understand-the-python/understanding-the-asterisk-of-python-8b9daaa4a558),或者你可以把它们分开。所以你可能想要这样的代码:

sql="从用户中选择密码,其中密码=%s";

values=(键,(

mycursor.execute(sql,values(

sql="从用户中选择密码WHERE password=%s";,(键,(

#函数调用中的星形运算符将序列变量按一个级别分解为单独的参数。

mycursor.execute(*sql(

最新更新