1

I'm trying to insert some data in my DB but I keep getting ( Unknown column 'None' in 'field list' ) error .

my code is :

cnx =mysql.connector.connect(user ='root', password = '',
                            host = '127.0.0.1', database = 'shop')
cursor = cnx.cursor()
cursor.execute(f'INSERT INTO customers (id, name, last_name) VALUES ({self.id}, \'{self.name}\', \'{self.lastname}\')')

and the table is :

+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id        | int         | NO   | PRI | NULL    |       |
| name      | varchar(10) | NO   |     | NULL    |       |
| last_name | varchar(10) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

it keep sayin Unknown column 'None' in 'field list' and I don't understand what is "None" because as far as I can see there is no "None" value

Alien
  • 13
  • 3
  • The code works for me - are you sure this is the code that is being executed? Note that using f-strings for SQL values is not a good idea - see the answers to [this question](https://stackoverflow.com/q/902408/5320906) for better ways - but that doesn't seem to be the problem here. – snakecharmerb Aug 11 '23 at 10:27
  • @snakecharmerb I have another class for items and I use same method for writing in my Db and it works correctly there ,but I have no idea what is wrong here. – Alien Aug 11 '23 at 10:42
  • There error message is consistent with the insert statement looking something like `INSERT INTO customers (id, name, None)`, where `None` is replacing one of the column names or is an extra "name". That's all I can tell you. – snakecharmerb Aug 11 '23 at 10:51
  • Use execute() method with placeholders instead of constructing the SQL statement. Since self.id is None the statement cannot be parsed: VALUES(None,'some_string','another_string') – Georg Richter Aug 11 '23 at 13:20

0 Answers0