1

I have a table called 'test' that I'm creating through my Python code.

db = sqlite3.connect("mydb.db")
cur = db.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS test 
            (
                "id" text primary key,
                "field1" integer,
                "fiedl2" integer,
                "field3" text
            )'''
           )

I have a file where each line is a json. I read the file line by line and insert data from the json of each line into the table.

file.txt:

{'id':'abcde', 'field1': 10, 'field2': 20, 'field3': "hello"}
{'id':'asdf', 'field1': 20, 'field2': 5, 'field3': "world"}
{'id':'qwerty', 'field1': 1, 'field2': 2, 'field3': None}

If a field in the json doesn't have a value, I want to insert null into my sqlite table. So as in the last line above, if field3 doesn't have a value, I put None into it because this is what I read in other related questions.

However, when I insert as below:

for line in readfile:
    line = ast.literal_eval(line)
    columns = '", "'.join([i.strip() for i in line.keys()])
    columns = "\"" + columns + '\"' 
    placeholders = ', '.join('?' * len(line))
    sql = 'INSERT INTO test ({}) VALUES ({})'.format(columns, placeholders)
    cur.execute(sql, ["'" + str(i) + "'" for i in line.values()])
    db.commit()

I get 'None' instead of null when I perform a select statement from the Sqlite console.

sqlite> select * from test where id='qwerty' limit 1;
'qwerty'|1|2|'None'|

Can anyone please help me fix this? I'm using Python 3.4

1
  • Have you considered testing if a value is None? Commented Nov 11, 2015 at 19:21

1 Answer 1

3

You're getting a "None" string, because you're converting the None into a string before passing it to the execute function:

cur.execute(sql, ["'" + str(i) + "'" for i in line.values()])

So, as far as python/sqlite is concerned, you're inserting the string "None" - not the None object.

The point of having the parameters as second argument in the execute function is that it's supposed to do the value-to-sql-string representation for you. Hence, you should provide the values in their native python datatypes.

So, using

cur.execute(sql, list(line.values()))

should actually give you a null instead of "None".

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

2 Comments

Works perfectly! I had to use cur.execute(sql, list(line.values()) though
Right - that's because you're using python3, where the values are returned as iterator - I missed that. I edited my answer accordingly.

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.