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

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

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

        • <bdo id='nuNrB'></bdo><ul id='nuNrB'></ul>
      1. 如何将 Python 连接到 Db2

        时间:2023-09-10

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

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

          • <small id='nlMOZ'></small><noframes id='nlMOZ'>

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

                1. <tfoot id='nlMOZ'></tfoot>
                    <tbody id='nlMOZ'></tbody>
                  本文介绍了如何将 Python 连接到 Db2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有没有办法将 Python 连接到 Db2?

                  Is there a way to connect Python to Db2?

                  推荐答案

                  文档很难找到,一旦找到,就非常糟糕.以下是我在过去 3 小时内发现的内容.

                  The documentation is difficult to find, and once you find it, it's pretty abysmal. Here's what I've found over the past 3 hours.

                  需要使用pip安装ibm_db,如下:

                  pip install ibm_db
                  

                  您需要创建一个连接对象.文档在这里.

                  You'll want to create a connection object. The documentation is here.

                  这是我写的:

                  from ibm_db import connect
                  # Careful with the punctuation here - we have 3 arguments.
                  # The first is a big string with semicolons in it.
                  # (Strings separated by only whitespace, newlines included,
                  #  are automatically joined together, in case you didn't know.)
                  # The last two are emptry strings.
                  connection = connect('DATABASE=<database name>;'
                                       'HOSTNAME=<database ip>;'  # 127.0.0.1 or localhost works if it's local
                                       'PORT=<database port>;'
                                       'PROTOCOL=TCPIP;'
                                       'UID=<database username>;'
                                       'PWD=<username password>;', '', '')
                  

                  接下来,您应该知道 ibm_db 的命令实际上永远不会给您结果.相反,您需要重复调​​用命令上的 fetch 方法之一来获取结果.我写了这个辅助函数来处理这个问题.

                  Next you should know that commands to ibm_db never actually give you results. Instead, you need to call one of the fetch methods on the command, repeatedly, to get the results. I wrote this helper function to deal with that.

                  def results(command):
                      from ibm_db import fetch_assoc
                  
                      ret = []
                      result = fetch_assoc(command)
                      while result:
                          # This builds a list in memory. Theoretically, if there's a lot of rows,
                          # we could run out of memory. In practice, I've never had that happen.
                          # If it's ever a problem, you could use
                          #     yield result
                          # Then this function would become a generator. You lose the ability to access
                          # results by index or slice them or whatever, but you retain
                          # the ability to iterate on them.
                          ret.append(result)
                          result = fetch_assoc(command)
                      return ret  # Ditch this line if you choose to use a generator.
                  

                  现在定义了该辅助函数,您可以轻松地执行以下操作,例如获取数据库中所有表的信息:

                  Now with that helper function defined, you can easily do something like get the information on all the tables in your database with the following:

                  from ibm_db import tables
                  
                  t = results(tables(connection))
                  

                  如果您想查看给定表格中的所有内容,您现在可以执行以下操作:

                  If you'd like to see everything in a given table, you could do something like this now:

                  from ibm_db import exec_immediate
                  
                  sql = 'LIST * FROM ' + t[170]['TABLE_NAME']  # Using our list of tables t from before...
                  rows = results(exec_immediate(connection, sql))
                  

                  现在 rows 包含数据库中第 170 个表中的行 list,其中每一行都包含列名的 dict:价值.

                  And now rows contains a list of rows from the 170th table in your database, where every row contains a dict of column name: value.

                  希望这一切都有帮助.

                  这篇关于如何将 Python 连接到 Db2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:解析用户名以提取用户位置 Twitter 下一篇:将 IBM_DB 与 Pandas 一起使用

                  相关文章

                  最新文章

                    <bdo id='0HZMH'></bdo><ul id='0HZMH'></ul>
                2. <i id='0HZMH'><tr id='0HZMH'><dt id='0HZMH'><q id='0HZMH'><span id='0HZMH'><b id='0HZMH'><form id='0HZMH'><ins id='0HZMH'></ins><ul id='0HZMH'></ul><sub id='0HZMH'></sub></form><legend id='0HZMH'></legend><bdo id='0HZMH'><pre id='0HZMH'><center id='0HZMH'></center></pre></bdo></b><th id='0HZMH'></th></span></q></dt></tr></i><div id='0HZMH'><tfoot id='0HZMH'></tfoot><dl id='0HZMH'><fieldset id='0HZMH'></fieldset></dl></div>
                3. <small id='0HZMH'></small><noframes id='0HZMH'>

                    <tfoot id='0HZMH'></tfoot>
                    <legend id='0HZMH'><style id='0HZMH'><dir id='0HZMH'><q id='0HZMH'></q></dir></style></legend>