所以基本上我想做的是创建一个将用户添加到数据库的函数。我正在使用 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 是一个列,但我真的不知道如何,因为它应该是一个值。