Python Forum
cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor'
#1
Hi,

I am creating a code which would export data from multiple tables in oracle to one excel file having each sheet as one tbale export. I have created 2 files for the same. Executing same on Windows 11.
One is config file as below.

# Configuration file for Oracle EXtracts
# Author: Mahesh Birajdar

#################### Oracle-- ODS
Oracle:

  dsn: localhost:1521/XEPDB1
  username: SALES
  password: Admin
And my python file is as below.

#!/usr/bin/env python
import cx_Oracle
import pandas as pd
import yaml 
import xlsxwriter

# Load config.yaml file
with open("config.yaml", "r") as f:
    config = yaml.safe_load(f)

# Get Oracle connection details
OracleuserID = config["Oracle"]["username"]
Oraclepassword = config["Oracle"]["password"]
Oracledsn = config["Oracle"]["dsn"]


def printheader():
    print('\n################### Oracle Extracts Automation Framework #########################')

def Oracle_connection(username,password,dsn):
    global conn
    global cur
    print('\n####################### Establishing Connections to Oracle DB ##############################')
    try:
        conn = cx_Oracle.connect(user=OracleuserID, password=Oraclepassword,
                               dsn=Oracledsn,
                               encoding="UTF-8")
        print('\nConnected to Oracle DB')
        cur=conn.cursor()

    except Exception as err:
        # logger.error('Error while connecting to Oracle {}'.format(err))
        print('\nError while connecting to Oracle')
        print(err)

printheader()

# Set the output file path
output_path = 'D:\Automation\Table_Export.xlsx'

# Create the connection to the Oracle database
Oracle_connection(OracleuserID,Oraclepassword,Oracledsn)

# Define the list of tables to retrieve data from
tables = ['OFFICER', 'CUSTOMER', 'PRODUCT']

# Export data from each table to a separate sheet in an Excel file
with pd.ExcelWriter(output_path) as writer:
    #for table in tables:
        # Define the SQL query to retrieve data from the table
        query = f"SELECT * FROM OFFICER ORDER BY INS_DTTM"

        # Use pandas to read the data from the Oracle database into a DataFrame
        dataframe = pd.read_sql(query, Oracle_connection)

        # Write the DataFrame to a new sheet in the Excel file
        dataframe.to_excel(writer, sheet_name=table, index=False)

# Close the database connection
print('\n####################### Closing Connections to Oracle DB ##############################')
cur.close()
conn.close()
print('\nDisconnected from Oracle DB')
But i am getting error as below.

Error:
PS D:\Automation> python .\Oracle_Extracts.py Traceback (most recent call last): File "D:\Automation\Oracle_Extracts.py", line 6, in <module> from sqlalchemy import create_engine ModuleNotFoundError: No module named 'sqlalchemy' PS D:\Automation> python .\Oracle_Extracts.py ################### Oracle Extracts Automation Framework ######################### ####################### Establishing Connections to Oracle DB ############################## Connected to Oracle DB D:\Automation\Oracle_Extracts.py:57: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. dataframe = pd.read_sql(query, Oracle_connection) Traceback (most recent call last): File "D:\Automation\Oracle_Extracts.py", line 57, in <module> dataframe = pd.read_sql(query, Oracle_connection) File "C:\Users\Mahes\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 633, in read_sql return pandas_sql.read_query( File "C:\Users\Mahes\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 2264, in read_query cursor = self.execute(sql, params) File "C:\Users\Mahes\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 2198, in execute cur = self.con.cursor() AttributeError: 'function' object has no attribute 'cursor'
Reply
#2
According to the documentation you should not use cx_oracle on Windows. You should use python-oracledb.

https://cx-oracle.readthedocs.io/en/late...on-windows

https://oracle.github.io/python-oracledb/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: audioio has no attribute 'AudioOut' netwrok 3 659 Oct-22-2023, 05:53 PM
Last Post: netwrok
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 791 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,758 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Need help with 'str' object is not callable error. Fare 4 861 Jul-23-2023, 02:25 PM
Last Post: Fare
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,522 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 765 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,929 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 755 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,685 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  Command error - cursor.executemany(comandoSQL,valoresInserir) TecInfo 2 1,361 Nov-18-2022, 01:57 PM
Last Post: TecInfo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020