Search code examples
pythonpyodbcattributeerror

Python 3.x: Error on pyodbc sql server connection "no attribute 'execute"


I keep getting the error: "'builtin_function_or_method' object has no attribute 'execute'" I originally thought the complaint was on the table value function in SQL Server, however, I see that the message points to "execute", so I don't think refcur has execute defined. Here's what my connection string looks like:

    conn = pyodbc.connect("Driver={SQL Server};"
                      "Server=myserver;"
                      "Database=mydatabase;"
                      "Trusted_Connection=yes;"
                      "autocommit=True;")

    refcur = conn.cursor
    sql = "exec myschema.mystoredproc @TVPobject=?"
    refcur.execute(sql,this_object)

I've attached the image to show what I see in intellisense for what's available. Does anyone know why this is happening?

enter image description here


Solution

  • you aren't calling cursor, you're just returning a reference to that member function and storing it in refcur. In order to call it (and actually create a cursor), you need to add parenthesis:

    refcur = conn.cursor()
    # Here -------------^