Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am tying to insert null value to a postgres timestamp datatype variable using python psycopg2.

The problem is the other data types such as char or int takes None, whereas the timestamp variable does not recognize None.

I tried to insert Null , null as a string because I am using a dictionary to get append the values for insert statement.

Below is the code.

queryDictOrdered[column] = queryDictOrdered[column] if isNull(queryDictOrdered[column]) is False else NULL

and the function is

def isNull(key):
    if str(key).lower() in ('null','n.a','none'):
        return True
    else:
        False

I get the below error messages:

DataError: invalid input syntax for type timestamp: "NULL"
DataError: invalid input syntax for type timestamp: "None"

share|improve this question

Surely you are adding quotes around the placeholder. Read psycopg documentation about passing parameters to queries.

share|improve this answer
    
the quotes around the null is to check the values before inserting !!! If you see the first piece of code I am trying to insert a NULL but since i am adding to a dictionary and then inserting the value I am not able to add NULL as a place holder but a string. So I need a work around to add null !! – Subu Ganesh Feb 22 at 20:26
    
use: queryDictOrdered[column] = queryDictOrdered[column] if not isNull(queryDictOrdered[column]) else None – piro Feb 23 at 10:55
    
You can insert None to Varchar variables but unfortunately, you cannot insert None to postgressql datetime variable. That is where I am having issues. I have found a work around of inserting dummy dates eg: '9999-12-12' but still not sure of how to insert null value :( – Subu Ganesh Feb 23 at 16:03
    
You are wrong: you can insert NULL to any Postgres data type, including dates and timestamps, and you can do that passing a None in psycopg. – piro Feb 23 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.