When using mysqldb and cursor.execute(), pass the value None, not "NULL":

value = None
cursor.execute("INSERT INTO table (`column1`) VALUES (%s)", (value,))

Found the answer here

Answer from j c on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › insert null into sql server using pyodbc
r/learnpython on Reddit: Insert null into sql server using pyodbc
February 21, 2024 -

Insert null value using pyodbc

Does anyone know how I can insert null values with pyodbc into sql server? The version of sql server I am using is 2019, and I’m using the latest odbc driver.

The connection and insert statements work, but when I try to insert None values, they get translated as some varchar value on the sql server side. I have seen some odbc attribute called sqldescribeparam that might determine the data type conversion into sql server, but I can’t figure out if or how I can access this via the pyodbc connection.

My connection looks something like how the documentation describes:

conn = pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};SERVER=test;DATABASE=test;UID=user;PWD=password')

🌐
Bytes
bytes.com › home › forum › topic › python
Python, Mysql, insert NULL - Post.Byes
If I have a Python variable that has a value None, and I want to > transmit this to MySQL as Null - I would: > > if somevar == None: > StrToConcatenat eIntoSqlStateme nt = "Null" > else: > StrToConcatenat eIntoSqlStateme nt = somevar > > All of which assumes, of course, that the field you are targeting will > accept a Null value. > Thomas Bartkus > >[/color] If you don't understand parameterized SQL queries you would do well to refrain from offering database advice :-) Presumably you always check whether StrToConcatenat eIntoSqlStateme nt contains no apostrophes before you actually construct the SQL?
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-insert-null-value-in-sqlalchemy
How to insert NULL value in SQLAlchemy? - GeeksforGeeks
January 28, 2022 - # specify null values as NONE statement6 = book_publisher.insert().values( publisherId=6, publisherName=None, publisherEstd=None) # insert the null values engine.execute(statement6) ...
🌐
Python
python-list.python.narkive.com › nytOfudl › python-mysql-insert-null
Python, Mysql, insert NULL
And also, NULL will be converted to None for SQL => Python direction. And to avoid unneccessary if-then-else you should follow his advice and use parametrized queries. I. e. cursor's execute method has two parameteres: 1) SQL query with placeholders 2) parameters For example: var1 = "Joe's dog" cur.execute("insert into mytable(col1) values (%s)", (var1,)) var1 = None cur.execute("insert into mytable(col1) values (%s)", (var1,)) if you use MySQLdb (the most sensible choice for a MySQL Python database adapter).
🌐
YouTube
youtube.com › codeglow
python sql insert null - YouTube
Instantly Download or Run the code at https://codegive.com title: python sql insert null tutorial with code exampleintroduction:in this tutorial, we will ex...
Published   February 23, 2024
Views   47
🌐
Atlassian Community
community.atlassian.com › q&a › confluence › questions › how to insert null value into sybase with python
Solved: How to insert null value into sybase with python
July 25, 2025 - How to handle Sybase table with ... as ('038C08234', null) (roolliup_indicator has null value) sql= "insert into Account(accountId, rollup_indicator) values(?, ?) " i......
🌐
Sada Tech
tech.sadaalomma.com › python › how-to-insert-null-value-in-sql-using-python-2
How to Insert Null Value in Sql Using Python - SADA Tech
March 3, 2024 - Learn to insert NULL values into SQL databases using Python with simple code examples. Perfect for database management and Python scripting.
Find elsewhere
🌐
GitHub
github.com › vertica › vertica-python › issues › 136
Cannot insert NULL as value into table · Issue #136 · vertica/vertica-python
November 6, 2016 - I am trying to insert NULL as value in a column based on a condition. The key is I only want it if a certain condition is true. Below is the code snippet: connection = vertica_python.connect(**self.vert_conn_info) cur = connection.cursor() insert_sql = """INSERT INTO {0}.{1} VALUES ('{2}', '{3}', {4})""" insert_sql_str = insert_sql.format(self.vert_schema_name, Config.VERT_TABLE_NAME_FOR_PLOT, self.run_date, exp_name, row.opu if not math.isnan(row.opu) else null ) cur.execute(insert_sql_str) cur.execute("commit;") Error: NameError: global name 'null' is not defined ·
Published   Dec 28, 2016
🌐
w3resource
w3resource.com › sql › insert-statement › insert-null.php
Sql inserting NULL values - w3resource
-- INSERT INTO statement begins INSERT INTO agents -- Specifies the table 'agents' where the data will be inserted VALUES ("A001","Jodi","London",.12,NULL); -- Specifies the values to be inserted into each column of the table: -- "A001" will be inserted into the 'agent_code' column (assuming it's a string data type) -- "Jodi" will be inserted into the 'agent_name' column -- "London" will be inserted into the 'working_area' column -- .12 will be inserted into the 'commission' column (assuming it's a numeric data type) -- NULL will be inserted into the 'salary' column, assuming it allows NULL values ... This SQL code attempts to perform an INSERT operation into the 'agents' table.
🌐
W3Schools
w3schools.com › sql › sql_null_values.asp
SQL NULL Values - IS NULL and IS NOT NULL
SQL Examples SQL Editor SQL Quiz SQL Exercises SQL Server SQL Syllabus SQL Study Plan SQL Bootcamp SQL Certificate SQL Training ... A field with a NULL value is a field with no value. If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field.
🌐
OneLinerHub
onelinerhub.com › python-mysql › how-do-i-insert-null-values-into-a-mysql-table-using-python
Python Mysql: How do I insert NULL values into a MySQL table using Python? - OneLinerHub
To insert NULL values into a MySQL table using Python, you can use the MySQL Connector Python library. The following code block shows an example of how to insert a NULL value into a MySQL table using the library: import mysql.connector mydb ...
🌐
Reddit
reddit.com › r/learnsql › sure way to ingest from csv( null values) to sql using python
r/learnSQL on Reddit: sure way to ingest from csv( null values) to sql using python
February 8, 2021 -

not sure if this is the place to ask.

assuming connections are ok.

will this have issue with null values if any. I've encountered many times that the null value will be painful. May I know if you have any suggestions or the usual practise to overcome this.

I know we can import over to the database BUT I need to automate via python in the future. Thanks

 # open the dataset csv file, skip the first row (header), and insert

each line as a record into the covid_data table

with open("data/nyccovid_{}.csv".format(date.today().strftime("%Y%m%d"))) as f: next(f) for row in f: cursor.execute(""" INSERT INTO covid_data VALUES ('{}', '{}', '{}', '{}') """.format(row.split(",")[0], row.split(",")[1], row.split(",")[2], row.split(",")[3]) )dbconnect.commit() cursor.close() dbconnect.close()

🌐
Python Forum
python-forum.io › thread-14139.html
How to handle None with mysql.connector
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='pas...