python连接oracle批量写入,如何使用Python批量插入Oracle数据库?

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?

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()

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.

EDIT: 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值