简体   繁体   English

即使行在数据库中,Python mySQL.connector 也返回 None

[英]Python mySQL.connector returns None even when row is in database

I'm making a server application where when a client connects, it determines whether it can send data or just listen to incoming data.我正在制作一个服务器应用程序,当客户端连接时,它确定它是可以发送数据还是只侦听传入数据。 It determines if it is a "sending" client via the following code:它通过以下代码确定它是否是“发送”客户端:

try:
    conn = mysql.connector.connect(host=config['databaseHost'], user=config['databaseUser'], password=config['databasePassword'], port=config['databasePort'])

except mysql.connector.Error as err:
    print(err)

cur = conn.cursor()
cur.execute("USE tcpPractice")
cur.execute("SELECT ip_address FROM client_list")
res = cur.fetchall()

for item in res:
    if item[0] not in senders:
        senders.append(item[0])

In the above code, if the address of the client is not in the list generated by res, it continues to the second part of the code, which is to determine if it is a listening client.上面代码中,如果客户端的地址不在res生成的列表中,则继续执行第二部分代码,即判断是否为监听客户端。 Here, clients that can listen are authenticated by username and password.在这里,可以侦听的客户端通过用户名和密码进行身份验证。 Here, I query the database for the given username and grab the password associated with it.在这里,我查询给定用户名的数据库并获取与之关联的密码。 However, when I run the below code to do so:但是,当我运行以下代码时:

socket.send("Enter your username: ".encode())
username = socket.recv(SIZE).decode()
socket.send("Enter your password: ".encode())
password = socket.recv(SIZE).decode()


statement = "SELECT passwd FROM users_table WHERE username = %s"

try:
    cur.execute(statement, (username, ))
except mysql.connector.Error as err:
    print(err)

hashed = cur.fetchone()

hashed always returns None, even when I test using a username and password that I know is in the database. hashed 总是返回 None,即使我使用我知道在数据库中的用户名和密码进行测试。 I tried closing the connection and cursor and recreating them to no avail, I made sure that syntax is correct, I made sure that I am using the correct database, I made sure that I am querying the correct table and looking for the correct column, I tried commiting after every execute statement, but nothing so far has worked.我尝试关闭连接和游标并重新创建它们无济于事,我确保语法正确,我确保我使用的是正确的数据库,我确保我正在查询正确的表并寻找正确的列,我尝试在每个执行语句之后提交,但到目前为止没有任何效果。

As a test, I tried the above in the Python shell (minus the lines related to the socket) and it works as intended there.作为测试,我在 Python shell 中尝试了上述内容(减去与套接字相关的行),并且在那里按预期工作。 I tried searching for this issue, but none of the solutions I looked at worked.我尝试搜索此问题,但我查看的所有解决方案均无效。 Any ideas what the issue might be?任何想法可能是什么问题?

EDIT: Forgot to mention that I am using netcat to connect to the server application, I'm not using any custom client program.编辑:忘记提及我使用 netcat 连接到服务器应用程序,我没有使用任何自定义客户端程序。

EDIT2: I just found that hardcoding the username like so: EDIT2:我刚刚发现像这样对用户名进行硬编码:

statement = "SELECT passwd FROM users_table WHERE username = 'testUser'"
cur.execute(statement)

works as intended and hashed no longer returns None, so I'm pretty sure the problem lies in either the prepared statement I'm trying to use or how I'm receiving the username from the client.按预期工作并且散列不再返回 None,所以我很确定问题在于我尝试使用的准备好的语句或我如何从客户端接收用户名。

EDIT3: I figured out what the issue was, I posted what the solution was. EDIT3:我找出了问题所在,我发布了解决方案。

I found out what the issue was.我发现了问题所在。 As it turns out, all the inputs from netcat had a \\n at the end of them, which was making the query fail.事实证明,来自 netcat 的所有输入的末尾都有一个\\n ,这导致查询失败。 So, doing this:所以,这样做:

socket.send("Enter your username: ".encode())
username = socket.recv(SIZE).decode().strip()
socket.send("Enter your password: ".encode())
password = socket.recv(SIZE).decode().strip()

fixed the issue and the server is now working as intended.修复了问题,服务器现在按预期工作。

change the statement to statement = "SELECT passwd FROM users_table WHERE username = '%s'"将语句更改为 statement = "SELECT passwd FROM users_table WHERE username = '%s'"

Is it missing the quotes?它是否缺少引号?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM