2

I am using following code to connect to the SQL Server and return the connection in order to be able to use in other function. However, I am getting an error of can't pickle pyodbc.Connection objects

The code is:

def connect_to_the_DB(**kwargs):
    #Connection to SQL Server
    conn = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}',
                          server='test.co.uk',
                          database='testdb',
                          uid='crm',
                          pwd='test123')
    print("Connection established")
    logging.info("Connection established")
    return conn

Any tips, how I can solve it?

Nazrin Guliyeva
  • 145
  • 1
  • 4
  • 14

1 Answers1

2

Some objects are not picklable (see Pickling unpicklable objects). I've had the same issue with Redis connections which seem similar to this, though I'm not sure.

The general advice is to extract the data you need and only pickle that, rather than the connection itself.

Brian
  • 115
  • 8