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

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

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

      1. 如何使用 Python 批量插入 Oracle 数据库?

        时间:2023-09-18
      2. <i id='2I4ld'><tr id='2I4ld'><dt id='2I4ld'><q id='2I4ld'><span id='2I4ld'><b id='2I4ld'><form id='2I4ld'><ins id='2I4ld'></ins><ul id='2I4ld'></ul><sub id='2I4ld'></sub></form><legend id='2I4ld'></legend><bdo id='2I4ld'><pre id='2I4ld'><center id='2I4ld'></center></pre></bdo></b><th id='2I4ld'></th></span></q></dt></tr></i><div id='2I4ld'><tfoot id='2I4ld'></tfoot><dl id='2I4ld'><fieldset id='2I4ld'></fieldset></dl></div>

          <tfoot id='2I4ld'></tfoot>
            <bdo id='2I4ld'></bdo><ul id='2I4ld'></ul>

                <tbody id='2I4ld'></tbody>

              <small id='2I4ld'></small><noframes id='2I4ld'>

                  <legend id='2I4ld'><style id='2I4ld'><dir id='2I4ld'><q id='2I4ld'></q></dir></style></legend>
                  本文介绍了如何使用 Python 批量插入 Oracle 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些每月的天气数据要插入到 Oracle 数据库表中,但我想批量插入相应的记录以提高效率.谁能建议我如何在 Python 中执行此操作?

                  I have some monthly weather data that I want to insert into an Oracle database table but I want to insert the corresponding records in a batch in order to be more efficient. Can anyone advise as to how I'd go about doing this in Python?

                  例如,假设我的表有四个字段:一个站 ID、一个日期和两个值字段.记录由站 ID 和日期字段(复合键)唯一标识.我必须为每个站插入的值将保存在一个包含 X 个完整年份数据的列表中,例如,如果有两年的值,那么值列表将包含 24 个值.

                  For example let's say my table has four fields: a station ID, a date, and two value fields. The records are uniquely identified by the station ID and date fields (composite key). The values I'll have to insert for each station will be kept in a list with X number of full years worth of data, so for example if there are two years of values then the value lists will contain 24 values.

                  我假设如果我想一次插入一个记录,我会这样做:

                  I assume that below is the way I'd do this if I wanted to insert the records one at a time:

                  connection_string = "scott/tiger@testdb"
                  connection = cx_Oracle.Connection(connection_string)
                  cursor = cx_Oracle.Cursor(connection)
                  station_id = 'STATION_1'
                  start_year = 2000
                  
                  temps = [ 1, 3, 5, 7, 9, 1, 3, 5, 7, 9, 1, 3 ]
                  precips = [ 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8 ]
                  number_of_years = len(temps) / 12
                  for i in range(number_of_years):
                      for j in range(12):
                          # make a date for the first day of the month
                          date_value = datetime.date(start_year + i, j + 1, 1)
                          index = (i * 12) + j
                          sql_insert = 'insert into my_table (id, date_column, temp, precip) values (%s, %s, %s, %s)', (station_id, date_value, temps[index], precips[index]))
                          cursor.execute(sql_insert)
                  connection.commit()
                  

                  有没有办法做我上面正在做的事情,但以执行批量插入的方式来提高效率?顺便说一句,我的经验是使用 Java/JDBC/Hibernate,所以如果有人可以给出与 Java 方法相比的解释/示例,那么它会特别有帮助.

                  Is there a way to do what I'm doing above but in a way that performs a batch insert in order to increase efficiency? BTW my experience is with Java/JDBC/Hibernate so if someone can give an explanation/example which compares to the Java approach then it'd be especially helpful.

                  也许我需要按照此处的描述使用 cursor.executemany()?

                  Perhaps I need to use cursor.executemany() as described here?

                  预先感谢您的任何建议、意见等

                  Thanks in advance for any suggestions, comments, etc.

                  推荐答案

                  以下是我提出的似乎效果很好的方案(但如果有改进的方法,请发表评论):

                  Here's what I've come up with which appears to work well (but please comment if there's a way to improve this):

                  # build rows for each date and add to a list of rows we'll use to insert as a batch 
                  rows = [] 
                  numberOfYears = endYear - startYear + 1
                  for i in range(numberOfYears):
                      for j in range(12):
                          # make a date for the first day of the month
                          dateValue = datetime.date(startYear + i, j + 1, 1)
                          index = (i * 12) + j
                          row = (stationId, dateValue, temps[index], precips[index])
                          rows.append(row)
                  
                  # insert all of the rows as a batch and commit
                  ip = '192.1.2.3' 
                  port = 1521
                  SID = 'my_sid'
                  dsn = cx_Oracle.makedsn(ip, port, SID)
                  connection = cx_Oracle.connect('username', 'password', dsn)
                  cursor = cx_Oracle.Cursor(connection)
                  cursor.prepare('insert into ' + database_table_name + ' (id, record_date, temp, precip) values (:1, :2, :3, :4)')
                  cursor.executemany(None, rows)
                  connection.commit()
                  cursor.close()
                  connection.close()
                  

                  这篇关于如何使用 Python 批量插入 Oracle 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Python sqlite3 中将现有的 db 文件加载到内存中? 下一篇:SQL Server 存储过程返回一个表

                  相关文章

                • <legend id='UuKGQ'><style id='UuKGQ'><dir id='UuKGQ'><q id='UuKGQ'></q></dir></style></legend>

                  1. <small id='UuKGQ'></small><noframes id='UuKGQ'>

                    <tfoot id='UuKGQ'></tfoot>

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