英文:
Cannot pickle connection object
问题
我使用以下代码连接到SQL Server并返回连接,以便在其他函数中使用。但是,我遇到了一个“无法pickle pyodbc.Connection对象”的错误。
这是代码:
def connect_to_the_DB(**kwargs):
# 连接到SQL Server
conn = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}',
server='test.co.uk',
database='testdb',
uid='crm',
pwd='test123')
print("连接已建立")
logging.info("连接已建立")
return conn
有什么提示,我如何解决这个问题?
英文:
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?
答案1
得分: 2
一些对象无法被序列化(参见 https://stackoverflow.com/questions/13691185/pickling-unpicklable-objects)。我曾经在Redis连接上遇到过类似的问题,尽管我不确定。
一般的建议是提取你需要的数据,然后只对数据进行序列化,而不是对连接本身进行序列化。
英文:
Some objects are not picklable (see https://stackoverflow.com/questions/13691185/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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论