从MS Access获取数据并插入Oracle SQL

qlfbtfca  于 2023-03-22  发布在  Oracle
关注(0)|答案(1)|浏览(181)

我正在创建一个Python代码,它从MS Access表中检索数据并将其插入Oracle SQL表中,但在使用INSERT INTO语句时,Oracle游标的 execute 函数出现了问题。
当我运行代码时,出现以下错误:**函数最多带2个参数(给定5个)**显然错误在 row[0]... 部分。但我不知道该怎么解决。
这是我的代码:

import pyodbc
import cx_Oracle

# Set up the Microsoft Access connection
access_conn_str = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
    r'DBQ=C:\MyFolder\ACC_BASE.MDB;'
    )
access_conn = pyodbc.connect(access_conn_str)

# Define the Oracle SQL connection string
oracle_conn_str = cx_Oracle.makedsn("MyConnection", "MyPort", "MySID")

# Create a connection to the Oracle SQL database
oracle_conn = cx_Oracle.connect(user="MyUser", password="MyPassword", dsn=oracle_conn_str)

# Create a cursor for each connection
access_cursor = access_conn.cursor()
oracle_cursor = oracle_conn.cursor()

# Execute the select statement to extract data from the Access table
access_cursor.execute('SELECT * FROM ACC_TABLE')

# Loop through the rows of the Access table and insert them into the Oracle SQL table
for row in access_cursor.fetchall():
    oracle_cursor.execute(
        'INSERT INTO ORACLE_TABLE (COD, LEV, AZET, HUES) VALUES (?, ?, ?, ?)',
        row[0], row[1], row[2], row[3]
    )

# Commit the changes to the Oracle SQL table
oracle_conn.commit()

# Close the cursors and connections
access_cursor.close()
access_conn.close()
oracle_cursor.close()
oracle_conn.close()
ovfsdjhp

ovfsdjhp1#

.execute()方法有两个参数。您提供了5个参数。第二个参数应该是包含您的参数的LIST:

oracle_cursor.execute(
    'INSERT INTO ORACLE_TABLE (COD, LEV, AZET, HUES) VALUES (?, ?, ?, ?)',
    [row[0], row[1], row[2], row[3]]
)

相关问题