Python Forum
How to handle None with mysql.connector
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to handle None with mysql.connector
#1
Hello everyone, Can anyone advice how to handle empty string or None when insert data in mysql integer column.
Here is the code i have:

import mysql.connector
cnx = mysql.connector.connect(user='user',
                                  password='pass',
                                  host='192.168.18.1',
                                  database='db')
data = None
conn = cnx.cursor()
conn.execute("insert into contract(red) values('{}')".format(data))
cnx.commit()
and here is the otput :
mysql.connector.errors.DataError: 1366 (22007): Incorrect integer value: 'None' for column 'red' at row 1
Problem is that i don't know when the data would be integer or None.
Reply
#2
You have two alternative options/paradigms: Ask for permission or Ask forgiveness, not permission
  • In the former you would check that data is indeed what you expect it to be, e.g. using if block
  • In the later you will use try/except block to handle gracefully the error if there is any
In most cases in python the preferred approach is the second one
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks for the reply.
The problem is that mysql.connector doesn't convert None or empty string to NULL so here is how i did it:
data = None or "NULL"
conn = cnx.cursor()
conn.execute("insert into contract(red) values({}) ".format(str(data)))
cnx.commit()
this way even if red column is integer it will insert the data correctly. It would be great if there is an easier way to do it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 682 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,086 Feb-09-2022, 09:55 AM
Last Post: ibreeden
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,689 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python ilknurg 3 5,628 Jan-18-2022, 06:25 PM
Last Post: ilknurg
  Program stuck at mysql.connector.connect zazas321 1 2,084 Jul-29-2021, 10:49 AM
Last Post: zazas321
  MYSQL.CONNECTOR ERROR DB1 8 3,848 Jul-23-2021, 03:31 AM
Last Post: DB1
  mysql.connector Nobima 1 1,900 Apr-10-2020, 12:00 PM
Last Post: Larz60+
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 3,058 Sep-17-2019, 05:21 PM
Last Post: swechsler
  Using VBA to Call a Python script causes error in pyodbc connector pcarra 1 2,825 Jun-11-2019, 04:14 PM
Last Post: pcarra
  Add a stored procedure with variable using MySQL Connector UtiliseIT 0 2,296 May-04-2019, 12:46 PM
Last Post: UtiliseIT

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020