2

In sqlite console,we can insert null simple.

create table data(code TEXT,date  DATETIME)
insert into data values("x1","20010101");
insert into data values("x2",null);

how to insert null with python-sqlite3?

import sqlite3 
db = r'F:\test.sqlite'
con = sqlite3.connect(db)
cur = con.cursor()
cur.execute('create table data(code TEXT,date  DATETIME)')
record1=('x1','2001-01-01')
cur.execute('INSERT INTO data VALUES(?,?);',record1)
record2=('x2','NULL')
cur.execute('INSERT INTO data VALUES(?,?);',record2)
record3=('x2','null')
cur.execute('INSERT INTO data VALUES(?,?);',record3)
record4=('x4',null)
cur.execute('INSERT INTO data VALUES(?,?);',record4)
record5=('x5',NULL)
cur.execute('INSERT INTO data VALUES(?,?);',record5)
con.commit()

cur.execute('INSERT INTO data VALUES(?,?);',record2)insert a string of NULL into table ,not null.
cur.execute('INSERT INTO data VALUES(?,?);',record3)insert a string of null into table ,not null.

Neither of cur.execute('INSERT INTO data VALUES(?,?);',record4) OR cur.execute('INSERT INTO data VALUES(?,?);',record5) can insert null into the table data.

How can i insert null into table with python-sqlite3?

1 Answer 1

9

The documentation says:

The following Python types can thus be sent to SQLite without any problem:

Python type   SQLite type
None          NULL
...
Sign up to request clarification or add additional context in comments.

1 Comment

record=('x',None) cur.execute('INSERT INTO data VALUES(?,?);',record)

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.