I
I
Ivan Yakushenko2019-07-17 23:06:29
Python
Ivan Yakushenko, 2019-07-17 23:06:29

How to compare None Python and NULL SQLite?

I perform parsing with entering data into the database, some elements during parsing can be None, so they are sent to the database, while they are entered into the database as NULL. The problem is that before adding an element to the database, I check for its presence:

cursor = await db.execute('SELECT * FROM MATCHES WHERE (a="{}" AND b="{}" AND c="{}".format(a, b, c)))

And here is the problem, when executing SELECT, it turns out that if a = None, then a != NULL and the record is a duplicate. I suspect that this is due to the fact that a query like a="None" goes to the database, i.e. string type.
Recording occurs with another request:
INSERT INTO MATCHES (a, b, c) VALUES (?,?,?)', (a, b, c)

Tried different syntax:
'SELECT * FROM MATCHES WHERE (a=? AND b=? AND c=?)', (a, b, c)

The result is the same.
How can INSERT be allowed to raise that None == NULL, given that any element (a, b, c) can be None and there are many more than 3?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Yakushenko, 2019-07-17
@kshnkvn

I solved the problem by replacing the = operator with IS

SELECT * FROM MATCHES WHERE (a IS ? AND b IS ? AND c IS ?)', (a, b, c)

A
Alexey Cheremisin, 2019-07-17
@leahch

And all because you need to use prepared statement! I suspect muscle zetcode.com/db/mysqlpython
Somewhere in between..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question