David Metcalfe
David Metcalfe

Reputation: 2431

SQLite ignore None on INSERT

I'm using an API that returns different dicts depending on the query. So because I can't be certain I'll have the desired keys, I'm using dict.get() to avoid raising a KeyError. But I am currently inserting these results into a database, and would like to avoid filling rows with None.

What would be the preferred way to deal with this?

Upvotes: 0

Views: 318

Answers (1)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48120

Use NULL as default value with dict.get(). In case key is not present in your dict object, it will return NULL instead of None. 'NULL' in databases (most) is equivalent to None in Python. For example:

>>> my_dict = {}

#                         v  Returns `NULL` if key not found in `my_dict`
>>> my_dict.get('key', 'NULL')
'NULL'

In case, you have column as NOT NULL, set them as empty string. For example:

>>> my_dict.get('key', '')
''

Upvotes: 1

Related Questions