Search code examples
postgresqlpython-2.7psycopg2

object has no attribute 'mogrify'


I am trying to insert data into a postgres DB using python package psycopg2.

def insertIntoDB(__insertSQL__,patternString,data,conn):
    conn = connectToDB()
    cur = conn.cursor  
    #print patternString
    insert_arg = ','.join(cur.mogrify("("+patternString+")",x) for x in data)
    cur.execute(__insertSQL__+insert_arg)
    conn.commit()   

When I execute the script I get the following error:

AttributeError: 'builtin_function_or_method' object has no attribute 'mogrify'

I have already installed the 64 bit installer for psycopg2 in my windows machine.

Any thoughts on this is much appreciated.


Solution

  • In this line:

    cur = conn.cursor
    

    you should be doing:

    cur = conn.cursor()
    

    What happens when you do the former is that you're not using a cursor you're using the built-in function or method which creates the cursor.