In python + sqlite3 "OperationalError: no such column:"

Introduction

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.

environment

python 3.7 sqlite 3.30.0

Status

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

problem

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~~~~

Conclusion

Be careful when embedding python variables in SQL queries.

Recommended Posts

In python + sqlite3 "OperationalError: no such column:"
Sqlite in python
OperationalError in SQLAlchemy + SQLite3
When issuing an INSERT statement in Python, no such column is displayed
How to use SQLite in Python
There is no switch in python
Foreign Key in Python SQLite [Note]
Revived from "no internet access" in Python
ModuleNotFoundError in Python: No module named story
Import Error in Python3: No module named'xxxxx'
Python in optimization
CURL in python
Output formatted output in Python, such as C / C ++ printf.
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
ModuleNotFoundError: No module named'_bz2' error in pyenv Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
What to do when [Errno 2] No such file or directory appears in Python
Csv in python
Disassemble in Python
Reflection in Python
Import cv2 ModuleNotFoundError: No module named'cv2' in python3
Parallel processing with no deep meaning in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Solution when module'XXX' has no attribute'XXX' in Python
How to handle datetime type in python sqlite3
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Implemented List and Bool in Python and SQLite3 (personal note)
[Python] No value for argument'self' in unbound method call
Specific sample code for working with SQLite3 in Python
Store Japanese (multibyte character string) in sqlite3 of python