When I try to add data to the database using sqlite3 in python, I get "Operational Error: no such column: None". From the conclusion, it seems that the grammar was inappropriate.
python 3.7 sqlite 3.30.0
Create a simple database to reproduce the error. Suppose the examples table has a column value of type integer. Store the value of the variable x created inside python as value.
import sqlite3
db_path="test.sqlite3"
con=sqlite3.connect(db_path)
c=con.cursor()
x=1
sql="insert into examples(value) values({0})".format(x)
c.execute(sql)
con.commit()
con.close()
If x = 1, no error will occur. But if x = None
#~~~Abbreviation~~~~
x=None
sql="insert into examples(value) values({0})".format(x)
c.execute(sql)
#~~~Abbreviation~~~~
OperationalError: no such column: None
The problem is that I used .format (), so "None" became a string. I noticed it was a stupid mistake, but I ate a lot of time ... To be correct, write as follows. This was also recommended in Official.
#~~~Abbreviation~~~~
x=None
sql="insert into examples(value) values(?)"
c.execute(sql, [x])
#~~~Abbreviation~~~~
Be careful when embedding python variables in SQL queries.
Recommended Posts