0

I have some Python code that inserts integer values to a database, as below:

if value: #if it's not None
    self._conn.execute(
    '''UPDATE table_name SET value_holder = ? WHERE
        key_one = ? AND key_two = ? and key_three = ?''',
        [value, key_one, key_two, key_three])

If I run that query, table will contain empty fields, or NULL fields whenever value is 0. If I change the query setup to below, however, the fields will contain 0 instead of NULL.

if value==0:
    self._conn.execute(
    '''UPDATE table_name SET value_holder = ? WHERE
        key_one = ? AND key_two = ? and key_three = ?''',
        [0, key_one, key_two, key_three])

elif value: #if it's not None
    self._conn.execute(
    '''UPDATE table_name SET value_holder = ? WHERE
        key_one = ? AND key_two = ? and key_three = ?''',
        [value, key_one, key_two, key_three])

How is it possible that inserting 0 vs inserting value (when value==0) results in different values in the database?

SQLite version 3.3.6.

6
  • 2
    if value will not be true when value is 0. Commented Aug 5, 2014 at 12:52
  • if value: #if it's not None. That's not how you check for None. if value is not None is how you do it. docs.python.org/2/library/stdtypes.html#truth-value-testing Commented Aug 5, 2014 at 12:54
  • Oh... I didn't realize that Python sees 0 as None in that regard. That cleared it up, thanks for the input. Commented Aug 5, 2014 at 13:02
  • 2
    It doesn't see 0 as None, but rather 0 evaluates to False Commented Aug 5, 2014 at 15:44
  • Nathaniel, yes, you are correct. Miswording on my part. Commented Aug 6, 2014 at 8:38

1 Answer 1

1

As user interjay answered: "if value will not be true when value is 0."

In my case, value was evaluated to false and the default null value was left unchanged in the database.

Supporting documentation posted by user netcoder: https://docs.python.org/2/library/stdtypes.html#truth-value-testing

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.