1

I would like to serialize results of the query. Here is my example:

import pypyodbc
import pickle

connection_string ='Driver={SQL Server Native Client 11.0};Server=localhost;' \
                       'Database=someDB;Uid=someLogin;Pwd=somePassword;'
connection = pypyodbc.connect(connection_string)
sql_query = "SELECT * FROM SomeTable"
cur = connection.cursor()
cur.execute(sql_query)
query_list = list(cur)

with open(r'D:\query_result', 'wb') as f:
    pickle.dump(query_list, f)
cur.close()
connection.close()

It generates the following error:

_pickle.PicklingError: Can't pickle <class 'pypyodbc.TupleRow.<locals>.Row'>: 
attribute lookup Row on pypyodbc failed

I guess pickle does not fully support pypyodbc objects. What would be a workaround?

user1700890
  • 7,144
  • 18
  • 87
  • 183

1 Answers1

2

I was able to recreate the issue using pypyodbc, while the same code seems to work fine with pyodbc. One possible workaround for pypyodbc might be to convert the results to a list of dictionary objects and then serialize that:

import pickle, pypyodbc
connection_string = (
    r"Driver={SQL Server Native Client 10.0};"
    r"Server=(local)\SQLEXPRESS;"
    r"Database=myDb;"
    r"Trusted_connection=yes;"
)
connection = pypyodbc.connect(connection_string)
cur = connection.cursor()
cur.execute("SELECT * FROM Donors")

column_names = [x[0] for x in cur.description]
query_list = [dict(zip(column_names, row)) for row in cur.fetchall()]

with open(r'C:\Users\Gord\Desktop\query_result', 'wb') as f:
    pickle.dump(query_list, f)
cur.close()
connection.close()
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418