嘗試將 df 中的資料插入 Oracle 資料庫表時遇到問題,這是錯誤:DatabaseError:ORA-01036:非法變數名稱/編號
這些是我做的步驟:
這是我從 yfinance 包中匯入并詳細闡述的資料框,以尊重我的 df 資料型別的完整性

我將我的 df 轉換成一個串列,這些是我在串列中的資料:

這是我要插入資料的表:

這是代碼:
sql_insert_temp = "INSERT INTO TEMPO('GIORNO','MESE','ANNO') VALUES(:2,:3,:4)"
index = 0
for i in df.iterrows():
cursor.execute(sql_insert_temp,df_list[index])
index = 1
connection.commit()
我在 sqldeveloper 作業表中嘗試了一個插入,使用您可以在串列中看到的資料,并且它起作用了,所以我想我在代碼中犯了一些錯誤。我看過其他討論,但我找不到任何解決我的問題的方法..你知道我如何解決這個問題,或者是否可以用另一種方式來解決這個問題?
I have tried to print the iterated queries and that's the result, that's why it's not inserting my data:

uj5u.com熱心網友回復:
如果你已經有一個pandas DataFrame,那么你應該可以使用to_sql()pandas 庫提供的方法。
import cx_Oracle
import sqlalchemy
import pandas as pd
DATABASE = 'DB'
SCHEMA = 'DEV'
PASSWORD = 'password'
connection_string = f'oracle://{SCHEMA}:{PASSWORD}@{DATABASE}'
db_conn = sqlalchemy.create_engine(connection_string)
df_to_insert = df[['GIORNO', 'MESE', 'ANNO']] #creates a dataframe with only the columns you want to insert
df_to_insert.to_sql(name='TEMPO', con=db_connection, if_exists='append')
- name 是表的名稱
- con 是連接物件
- if_exists='append' 會將行添加到表的末尾。還有其他選項可以添加失敗或洗掉并重新創建表
其他引數可以在 pandas 網站上找到。pandas.to_sql()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/333126.html
標籤:python pandas oracle list dataframe
下一篇:Oracle-將字串轉換為時間戳
