Search code examples
pythoncx-oraclepython-polars

Reading SQL query using Polars and cx_Oracle gives error


I'm trying to read a SQL-query using the python library Polars. Firstly, I establish a connection using the cx_Oracle library as follows:

import polars as ps
import cx_Oracle as oracle

user = XXX
host = XXX
port = XXX
service_name = XXX
password = XXX
dsnStr = oracle.makedsn(host, port, service_name)
gds_con = oracle.connect(user, password, dsnStr)

query = XXX

Then, I want to make use of Polars:

result = ps.read_database(query, gds_con)

But the latter results in the following error:

'cx_Oracle.Connection' object has no attribute 'split'

How can this issue be resolved?


Solution

  • polars uses connectorx to make database connections. It doesn't use a cx_Oracle connection object as you're trying to feed it. It uses a connection string. From here I found this example format of the connection string:

    cstr = f"oracle://{user}:{password}@{host}:{port}"
    

    or maybe

    cstr = f"oracle://{user}:{password}@{host}:{port}/{service_name}"
    

    Then you can do

    import polars as pl
    result = pl.read_database(query, cstr)