Вопрос

So basically what I'm trying to do is create a function that adds a user to a database. I'm using pymysql to connect to my database.

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')

The error I'm getting is

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'")

It seems to think Admin is a column but I don't really know how since it's supposed to be a value.

Это было полезно?

Решение

I am not a python programmer, but you can try to put %s values like this:

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

Другие советы

I was facing a similar problem - possibly there might be a typo on the name of the column in the database (vis-a-vis your code)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top