Get data from MS Access and insert into Oracle SQL

Question:

I’m creating a Python code that retrieves data from an MS Access table and inserts it into an Oracle SQL table, but I’m having problems with the execute function of the Oracle cursor when using INSERT INTO statement.

When I run the code, the following error appears: function takes at most 2 arguments (5 given)
Apparently the error is in row[0]… part. But I don’t know what to do to solve it.

this is my code:

import pyodbc
import cx_Oracle

# Set up the Microsoft Access connection
access_conn_str = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
    r'DBQ=C:MyFolderACC_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()
Asked By: Req_7

||

Answers:

.execute() method takes two arguments. You’ve supplied 5. The second argument should be a LIST containing your parameters:

oracle_cursor.execute(
    'INSERT INTO ORACLE_TABLE (COD, LEV, AZET, HUES) VALUES (?, ?, ?, ?)',
    [row[0], row[1], row[2], row[3]]
)
Answered By: JNevill
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.