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

        <bdo id='zDgku'></bdo><ul id='zDgku'></ul>
    1. <tfoot id='zDgku'></tfoot>

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

        使用 python 数据集访问和关闭 postgres 数据库的最佳方法

        Best way to access and close a postgres database using python dataset(使用 python 数据集访问和关闭 postgres 数据库的最佳方法)
          <tbody id='mBO7W'></tbody>
      2. <small id='mBO7W'></small><noframes id='mBO7W'>

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

          • <bdo id='mBO7W'></bdo><ul id='mBO7W'></ul>

                <legend id='mBO7W'><style id='mBO7W'><dir id='mBO7W'><q id='mBO7W'></q></dir></style></legend>
                  本文介绍了使用 python 数据集访问和关闭 postgres 数据库的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  import dataset    
                  from sqlalchemy.pool import NullPool
                  
                  db = dataset.connect(path_database, engine_kwargs={'poolclass': NullPool})
                  
                  table_f1 = db['name_table']
                  # Do operations on table_f1
                  
                  db.commit()
                  db.executable.close()
                  

                  我使用这段代码来访问一个 postgres 数据库并且有时会写入它.最后,我关闭它.上面的代码是访问和关闭它的最佳方式吗?或者,下面的代码更好吗?

                  I use this code to access a postgres database and sometimes write to it. Finally, I close it. Is the above code the best way to access and close it? Alternatively, is the code below better?

                  import dataset    
                  from sqlalchemy.pool import NullPool
                  
                  with dataset.connect(path_database, engine_kwargs={'poolclass': NullPool}) as db:
                      table_f1 = db['name_table']
                      # Do operations on table_f1
                  
                      db.commit()
                  

                  特别是,我希望 100% 确保在完成这段代码后不会连接到 postgres 数据库.实现它的更好方法是什么?选项 1 还是选项 2?

                  In particular, I want to make 100% sure that there is no connection to the postgres database once this piece of code is done. Which is the better way to achieve it? option 1 or option 2?

                  推荐答案

                  目前,主要问题是选项 2 中使用的上下文管理器(with 语句)没有处理连接,只有 事务(提交/回滚块的末尾).

                  For now, the main issue is that the context manager used in Option 2 (with statement) doesn't handle the connection, only the transaction (commit/rollback at the end of the block).

                  (这个问题已经报告给Github repo,也许这个行为会改变?)

                  (This question is already reported to the Github repo, maybe the behavior will change ?)

                  所以你应该在选项 2 中将 db.commit() 替换为 db.executable.close():

                  So you should replace db.commit() by db.executable.close() in Option 2:

                  import dataset    
                  from sqlalchemy.pool import NullPool
                  
                  with dataset.connect(path_database, engine_kwargs={'poolclass': NullPool}) as db:
                      table_f1 = db['name_table']
                      print(db.local.conn.closed) # >>>False
                  
                      # Do operations on table_f1
                      # end of the context manager, trying to commit 
                  
                  db.executable.close()
                  print(db.local.conn.closed) # >>>True
                  

                  现在连接已关闭:

                  # db['name_table'].all() ==> throws an error due to closed connection
                  

                  但是...您仍然可以在数据库中创建新表(因为元数据?):

                  BUT... you can still create new tables in the database (because of Metadata ?) :

                  # db['new_table'] ==> enough to add a new table 
                  

                  因此,您可能希望销毁所有内容以防止这种情况发生(db = Nonedb.metadata = None)

                  So you may want to destroy everything to prevent this (db = None, or db.metadata = None)

                  最后一个行为也发生在 SQLAlchemy 中:

                  This last behavior happens in SQLAlchemy too:

                  from sqlalchemy import *
                  from sqlalchemy.pool import NullPool
                  
                  engine = create_engine('postgresql:///datatest', poolclass=NullPool) 
                  
                  connection = engine.connect()
                  meta = MetaData(engine)
                  t1 = Table('Table_1', meta,
                             Column('id', Integer, primary_key=True),
                             Column('name',String))
                  t1.create()
                  connection.close()
                  
                  t2 = Table('Table_2', meta,
                             Column('id', Integer, primary_key=True),
                             Column('name',String))
                  t2.create()
                  # table_2 is created in database
                  

                  (感谢 Ilja Everil 的评论,以及对 的关注文档)

                  (thanks to Ilja Everil's comment, and a focus on the doc)

                  最好调用 meta = MetaData(connection) 以便在引擎处理时关闭连接,这将在上面的示例中引发错误,连接已关闭.

                  Better call meta = MetaData(connection) in order to close the connection at the engine disposal, this will raise an error in the above example, connection IS closed.

                  这篇关于使用 python 数据集访问和关闭 postgres 数据库的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                    <tbody id='exJyz'></tbody>

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

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

                    • <tfoot id='exJyz'></tfoot><legend id='exJyz'><style id='exJyz'><dir id='exJyz'><q id='exJyz'></q></dir></style></legend>

                          <bdo id='exJyz'></bdo><ul id='exJyz'></ul>