无法序列化连接对象

huangapple go评论112阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月6日 22:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定