首页 > 解决方案 > The shorter code to insert data from Python into Oracle

问题描述

I need to insert data from Python into Oracle. My table in Oracle has about 30 columns. I would like to know if there is a way to use the shorter code in next 3 cases.

case 1

Only 30 columns in my_table and 30 values in data

cur = con.cursor() 
cur.executemany('''
insert into my_table 
values (:1,:2,...,:30)
''', data)
con.commit()
cur.close()

case 2

30 columns + 1 column with default sysdate in my_table and 30 values in data

cur = con.cursor() 
cur.executemany('''
insert into my_table 
(col1, col2,..., col30)
values (:1,:2,...,:30)
''', data)
con.commit()
cur.close()

case 3

30 columns in my_table and 10 values in data

cur = con.cursor() 
cur.executemany('''
insert into my_table 
(col1, col2,..., col10)
values (:1,:2,...,:10)
''', data)
con.commit()
cur.close()

Is there a way not to list all 30 columns? Something like values(:) for case 1 or values(:1 - :10) for case 3 How can I specify all columns except default sysdate in case 2?

标签: pythonoracle

解决方案


推荐阅读