2
votes

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

1 Answers

9
votes

The documentation says:

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

Python type   SQLite type
None          NULL
...