简体   繁体   English

为 cx_Oracle 动态使用 INSERT - Python

[英]Dynamically using INSERT for cx_Oracle - Python

I've been looking around so hopefully someone here can assist:我一直在四处寻找,希望这里有人可以提供帮助:

I'm attempting to use cx_Oracle in python to interface with a database;我正在尝试使用 python 中的 cx_Oracle 与数据库进行交互; my task is to insert data from an excel file to an empty (but existing) table.我的任务是将数据从 excel 文件插入到一个空的(但存在的)表中。

I have the excel file with almost all of the same column names as the columns in the database's table, so I essentially want to check if the columns share the same name;我有 excel 文件,其中几乎所有列名都与数据库表中的列相同,所以我基本上想检查列是否共享相同的名称; and if so, I insert that column from the excel (dataframe --pandas) file to the table in Oracle.如果是这样,我将该列从 excel (dataframe --pandas) 文件插入到 Oracle 中的表中。

import pandas as pd
import numpy as np
import cx_Oracle

df = pd.read_excel("employee_info.xlsx")

con = None
try:
    con = cx_Oracle.connect (
          config.username,
          config.password,
          config.dsn,
          encoding = config.encoding)
except cx_Oracle.Error as error:
      print(error)
finally:
       cursor = con.cursor()
       rows = [tuple(x) for x in df.values]
       cursor.executemany( ''' INSERT INTO ODS.EMPLOYEES({x} VALUES {rows})   '''

I'm not sure what sql I should put or if there's a way I can use a for-loop to iterate through the columns but my main issue stems from how can I dynamically add these for when our dataset grows in columns?我不确定我应该放什么 sql 或者是否有办法可以使用 for 循环遍历列,但我的主要问题源于当我们的数据集在列中增长时如何动态添加这些列?

I check the columns that match by using:我使用以下方法检查匹配的列:

sql = "SELECT * FROM ODS.EMPLOYEES"
cursor.execute(sql)
data = cursor.fetchall()
col_names = []
for i in range (0, len(cursor.description)):
    col_names.append(cursor.description[i][0])

a = np.intersect1d(df.columns, col_names)
print("common columns:", a)

that gives me a list of all the common columns;这给了我所有常见列的列表; so I'm not sure?所以我不确定? I've renamed the columns in my excel file to match the columns in the database's table but my issue is that how can I match these in a dynamic/automated way so I can continue to add to my datasets without worrying about changing the code.我已重命名我的 excel 文件中的列以匹配数据库表中的列,但我的问题是如何以动态/自动方式匹配这些列,以便我可以继续添加到我的数据集而不必担心更改代码。

Bonus : I also am importing SQL in a case statement to create a new column where I'm rolling up a few other columns;奖励:我还在 case 语句中导入 SQL 以创建一个新列,我正在其中汇总其他一些列; if there's a way to add this to the first part of my SQL or if it's advisable to do all manipulations before using an insert statement that'll be helpful to know as well.如果有一种方法可以将其添加到我的 SQL 的第一部分,或者是否建议在使用插入语句之前进行所有操作,这也有助于了解。

Look at https://github.com/oracle/python-oracledb/blob/main/samples/load_csv.pyhttps://github.com/oracle/python-oracledb/blob/main/samples/load_csv.py

You would replace the CSV reading bit with parsing your data frame.您将用解析数据帧替换 CSV 读取位。 You need to construct a SQL statement similar to the one used in that example:您需要构建类似于该示例中使用的语句的 SQL 语句:

sql = "insert into LoadCsvTab (id, name) values (:1, :2)"

For each spreadsheet column that you decide matches a table column, construct the (id, name) bit of the statement and add another id to the bind section (:1, :2) .对于您决定与表列匹配的每个电子表格列,构造语句的(id, name)位并将另一个 id 添加到绑定部分(:1, :2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM