L
Lee Harr
When I am retrieving rows using psycopg, a NULL value
from the database will be translated to python None.
What I cannot figure out is how to insert a NULL.
import psycopg
DSN = 'dbname=test6 user=auth1'
conn = psycopg.connect(DSN)
curs = conn.cursor()
curs.execute('CREATE TABLE foo (i integer)')
conn.commit()
# These all work
curs.execute('INSERT INTO foo (i) VALUES (%s)' % 5)
curs.execute('INSERT INTO foo (i) VALUES (%i)' % 4)
curs.execute('INSERT INTO foo (i) VALUES (%d)' % 3)
conn.commit()
# None of these work ...
#curs.execute('INSERT INTO foo (i) VALUES (%s)' % None)
#curs.execute('INSERT INTO foo (i) VALUES (%d)' % None)
#curs.execute('INSERT INTO foo (i) VALUES (%i)' % None)
#conn.commit()
# If I insert NULL some other way, this works
curs.execute('SELECT * FROM foo')
rows = curs.fetchall()
for row in rows:
if row[0] is None:
print 'None!'
Any suggestions?
from the database will be translated to python None.
What I cannot figure out is how to insert a NULL.
import psycopg
DSN = 'dbname=test6 user=auth1'
conn = psycopg.connect(DSN)
curs = conn.cursor()
curs.execute('CREATE TABLE foo (i integer)')
conn.commit()
# These all work
curs.execute('INSERT INTO foo (i) VALUES (%s)' % 5)
curs.execute('INSERT INTO foo (i) VALUES (%i)' % 4)
curs.execute('INSERT INTO foo (i) VALUES (%d)' % 3)
conn.commit()
# None of these work ...
#curs.execute('INSERT INTO foo (i) VALUES (%s)' % None)
#curs.execute('INSERT INTO foo (i) VALUES (%d)' % None)
#curs.execute('INSERT INTO foo (i) VALUES (%i)' % None)
#conn.commit()
# If I insert NULL some other way, this works
curs.execute('SELECT * FROM foo')
rows = curs.fetchall()
for row in rows:
if row[0] is None:
print 'None!'
Any suggestions?