在cx_Oracle中使用with cursor on cursor

时间:2015-12-11 16:15:52

标签: python with-statement cx-oracle

cx_Oracle在Connection对象上包含__enter____exit__,但在Cursor对象上不包含class CursorWrapper(object): def __init__(self, connection): self.connection = connection self.cursor = None def __enter__(self): self.cursor = self.connection.cursor() return self.cursor def __exit__(self, exc_type, exc_value, traceback): self.cursor.close() 。因此,我到处使用它来包装游标:

with CursorWrapper(cnx) as cursor:
    cursor.execute("whatever sql statement")

然后,当我想要一个光标

__enter__

它非常适合我的需要。

然而,我当时想知道什么可以阻止在{cx_Oracle中直接添加__exit__import contextlib with contextlib.closing(cnx.cursor()) as cursor: 方法?

或者是否有更好的方法将游标与上下文管理结合使用,这可以解释为什么它没有在模块中定义?

编辑: 对不起,我刚刚找到答案。我可以使用contextlib.closing。

{
   "Method not found: 
  'System.Threading.Tasks.Task`1<System.Collections.Generic.List`1<Microsoft.TeamFoundation.Build.WebApi.Change>> 
   Microsoft.TeamFoundation.Build.WebApi.BuildHttpClientBase.GetChangesBetweenBuildsAsync(
    System.String, 
    System.Nullable`1<Int32>,
    System.Nullable`1<Int32>, 
    System.Nullable`1<Int32>, 
    System.Object, 
    System.Threading.CancellationToken)'."
}

0 个答案:

没有答案
相关问题