gpt4 book ai didi

mysql - “字段”中的 Python pymysql 未知列

转载 作者:太空宇宙 更新时间:2023-11-03 10:56:35 24 4
gpt4 key购买 nike

基本上我想做的是创建一个将用户添加到数据库的函数。我正在使用 pymysql 连接到我的数据库。

import pymysql
from passlib.hash import sha256_crypt

try:
conn = pymysql.connect(host='', unix_socket='/tmp/mysql.sock', user='', passwd='', db='')
cur = conn.cursor()
except Exception as e:
print("Fail", e)

def register(username, password):
hashe = sha256_crypt.encrypt(password)
cur.execute("INSERT INTO users (username, password) VALUES (%s, %s)" % (username, hashe))

register('Admin','password')

我得到的错误是

Traceback (most recent call last):
File "db.py", line 20, in <module>
register('Admin','password')
File "db.py", line 16, in register
cur.execute("INSERT INTO users (username, password) VALUES (%s, %s)" % (username, hashe))
File "F:\Python33\lib\site-packages\pymysql\cursors.py", line 102, in execute
result = self._query(query)
File "F:\Python33\lib\site-packages\pymysql\cursors.py", line 202, in _query
conn.query(q)
File "F:\Python33\lib\site-packages\pymysql\connections.py", line 734, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "F:\Python33\lib\site-packages\pymysql\connections.py", line 845, in _read_query_result
result.read()
File "F:\Python33\lib\site-packages\pymysql\connections.py", line 1049, in read
first_packet = self.connection._read_packet()
File "F:\Python33\lib\site-packages\pymysql\connections.py", line 826, in _read_packet
packet.check_error()
File "F:\Python33\lib\site-packages\pymysql\connections.py", line 373, in check_error
raise_mysql_exception(self.__data)
File "F:\Python33\lib\site-packages\pymysql\err.py", line 117, in raise_mysql_exception
_check_mysql_exception(errinfo)
File "F:\Python33\lib\site-packages\pymysql\err.py", line 113, in _check_mysql_exception
raise InternalError(errno, errorvalue)
pymysql.err.InternalError: (1054, "Unknown column 'Admin' in 'field list'")

它似乎认为 Admin 是一个列,但我真的不知道如何,因为它应该是一个值。

最佳答案

我不是 python 程序员,但你可以尝试像这样输入 %s 值:

INSERT INTO users (username, password) VALUES ('%s', '%s')

关于mysql - “字段”中的 Python pymysql 未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20134376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com