【问题标题】:How to insert data into Oracle Database using Pandas?如何使用 Pandas 将数据插入 Oracle 数据库?
【发布时间】:2021-02-27 12:23:36
【问题描述】:

嗨,下面是我想要做的代码,连接字符串很好,当我运行这个时,没有错误弹出,也没有更新数据库 你能帮我找出错误吗?

我有两张表里面的excel数据是相同的,只是第二张表名称中的另一列与性别相同

sample Dataframe which i want to insert into DB is 
    NAME  AGE
0    x   21
1    y   22
2    z   23
3    m   24
4    t   35



import cx_Oracle
from sqlalchemy import  create_engine

engine = Create_engine('oracle+cx_oracle://<Connection Detail>')

DB_Connection = cx_Oracle.connect(<Connection Detail)
mycursor = DB_Connection.cursor()

sample_excel = pd.ExcelFile(r<file Path>)
sample_excel_sheet = sample_excel.sheet_names


for i in sample_excel_sheet:
  
    sheet_data = pd.read_excel(r<file Path>,sheet_name=i)

    sheet_namee = i.upper()
    
    with engine.connect() as connection:                
            sheet_data.to_sql(name='sheet_namee',con=engine, if_exists='replace')  
        
                
                
DB_Connection.commit()

【问题讨论】:

  • sheet_namee 如果工作表名称为 MYSHEET,则返回“MYSHEET”。您是否尝试过在 ´´´sheet_data.to_sql(name='sheet_namee',con=engine, if_exists='replace')``` 中简单地写 name=sheet_namee
  • 嗨,谢谢您的回复,是的,我确实尝试过,如果我通过不带引号的 sheet_namee,它将在 DB 中创建一个名为 sheet_namee 的表
  • 甚至我尝试打印 to_sql 语句,同时将其存储在打印为 NONE 的 var 中。不知道为什么?

标签: python pandas sqlalchemy cx-oracle


【解决方案1】:

你只需要改变

name='sheet_namee'

name=sheet_namee

因为sheet_namee = i.upper() 已经返回带引号的工作表名称。

【讨论】:

  • 我明白谢谢,但是 name=sheet_namee 将小写的工作表名称返回为大写,它没有返回带引号的工作表名称
猜你喜欢
  • 2018-05-12
  • 1970-01-01
  • 2015-11-25
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
相关资源
最近更新 更多