• <i id='gqf42'><tr id='gqf42'><dt id='gqf42'><q id='gqf42'><span id='gqf42'><b id='gqf42'><form id='gqf42'><ins id='gqf42'></ins><ul id='gqf42'></ul><sub id='gqf42'></sub></form><legend id='gqf42'></legend><bdo id='gqf42'><pre id='gqf42'><center id='gqf42'></center></pre></bdo></b><th id='gqf42'></th></span></q></dt></tr></i><div id='gqf42'><tfoot id='gqf42'></tfoot><dl id='gqf42'><fieldset id='gqf42'></fieldset></dl></div>
      <bdo id='gqf42'></bdo><ul id='gqf42'></ul>

        <small id='gqf42'></small><noframes id='gqf42'>

        <legend id='gqf42'><style id='gqf42'><dir id='gqf42'><q id='gqf42'></q></dir></style></legend>

      1. <tfoot id='gqf42'></tfoot>

        使用SQLAlChemy会话作为上下文管理器时,接收&quot;属性错误:__Enter__&Quot;

        时间:2024-04-20
        <i id='qpZxk'><tr id='qpZxk'><dt id='qpZxk'><q id='qpZxk'><span id='qpZxk'><b id='qpZxk'><form id='qpZxk'><ins id='qpZxk'></ins><ul id='qpZxk'></ul><sub id='qpZxk'></sub></form><legend id='qpZxk'></legend><bdo id='qpZxk'><pre id='qpZxk'><center id='qpZxk'></center></pre></bdo></b><th id='qpZxk'></th></span></q></dt></tr></i><div id='qpZxk'><tfoot id='qpZxk'></tfoot><dl id='qpZxk'><fieldset id='qpZxk'></fieldset></dl></div>
            <tbody id='qpZxk'></tbody>
        • <small id='qpZxk'></small><noframes id='qpZxk'>

          <tfoot id='qpZxk'></tfoot>

          1. <legend id='qpZxk'><style id='qpZxk'><dir id='qpZxk'><q id='qpZxk'></q></dir></style></legend>
              <bdo id='qpZxk'></bdo><ul id='qpZxk'></ul>
                1. 本文介绍了使用SQLAlChemy会话作为上下文管理器时,接收&quot;属性错误:__Enter__&Quot;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我收到带有以下内容的AttributeError: __enter__。这与with Session(engine) as session相关:

                  from sqlalchemy import create_engine
                  from sqlalchemy import text
                  from sqlalchemy.orm import Session
                  from sqlalchemy import MetaData
                  from sqlalchemy import Table, Column, Integer, String
                  from sqlalchemy import ForeignKey
                  
                  engine = create_engine("sqlite+pysqlite:///:memory:", echo=True)
                  
                  with engine.connect() as conn:
                      conn.execute(text("CREATE TABLE some_table (x int, y int)"))
                      conn.execute(text("INSERT INTO some_table (x, y) VALUES (:x, :y)"),[{"x": 1, "y": 1}, {"x": 2, "y": 4}])
                  
                  with engine.begin() as conn:
                      conn.execute(text("INSERT INTO some_table (x, y) VALUES (:x, :y)"),[{"x": 6, "y": 8}, {"x": 9, "y": 10},
                      {"x": 11, "y": 12}, {"x": 13, "y": 14}])
                  
                  with engine.connect() as conn:
                      result = conn.execute(text("Select x,y From some_table"))
                      for x, y in result:
                          print(f"x:{x} y:{y}")
                  
                  stmt = text("SELECT x, y FROM some_table WHERE y > :y ORDER BY x, y").bindparams(y=6)
                  
                  with Session(engine) as session:
                      result = session.execute(stmt)
                      for row in result:
                          print(f'x: {row.x}  y: {row.y}')
                  

                  我使用的是蟒蛇1.3.23附带的SQLAlChemy版本。

                  推荐答案

                  通过上下文管理器运行会话构建/关闭进程,如下所示:

                  engine = create_engine(...)
                  Session = sessionmaker(bind=engine)
                  
                  with Session() as session:
                      session.add(something)
                      session.commit()
                  

                  在SQLAlChemy<;1.4上不受支持。

                  如果您的SQLAlChemy版本为例如1.3.x,则应改为:

                  engine = create_engine(...)
                  Session = sessionmaker(bind=engine)
                  
                  session = Session()
                  session.add(something)
                  session.commit()
                  

                  如果您确实想使用上下文管理器,同时又需要使用SQLAlChemy<;1.4,您可以使用以下方法(复制自SQLAlchemy docs):

                  ### another way (but again *not the only way*) to do it ###
                  
                  from contextlib import contextmanager
                  
                  @contextmanager
                  def session_scope():
                      """Provide a transactional scope around a series of operations."""
                      session = Session()
                      try:
                          yield session
                          session.commit()
                      except:
                          session.rollback()
                          raise
                      finally:
                          session.close()
                  
                  
                  def run_my_program():
                      with session_scope() as session:
                          ThingOne().go(session)
                          ThingTwo().go(session)
                  

                  这篇关于使用SQLAlChemy会话作为上下文管理器时,接收&quot;属性错误:__Enter__&Quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQLAlChemy(mental copg2.ProgrammingError)不能适配类型。 下一篇:如何在SQLAlChemy中按自定义函数排序

                  相关文章

                2. <small id='pZAIX'></small><noframes id='pZAIX'>

                3. <tfoot id='pZAIX'></tfoot>
                4. <legend id='pZAIX'><style id='pZAIX'><dir id='pZAIX'><q id='pZAIX'></q></dir></style></legend>

                    <bdo id='pZAIX'></bdo><ul id='pZAIX'></ul>
                  1. <i id='pZAIX'><tr id='pZAIX'><dt id='pZAIX'><q id='pZAIX'><span id='pZAIX'><b id='pZAIX'><form id='pZAIX'><ins id='pZAIX'></ins><ul id='pZAIX'></ul><sub id='pZAIX'></sub></form><legend id='pZAIX'></legend><bdo id='pZAIX'><pre id='pZAIX'><center id='pZAIX'></center></pre></bdo></b><th id='pZAIX'></th></span></q></dt></tr></i><div id='pZAIX'><tfoot id='pZAIX'></tfoot><dl id='pZAIX'><fieldset id='pZAIX'></fieldset></dl></div>