Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CX_Oracle - import data from Oracle to Pandas dataframe

Hy,

I'm new in python and I want import some data from a Oracle Database to python (pandas dataframe) using this simple query

SELECT*                  FROM TRANSACTION                 WHERE DIA_DAT >=to_date('15.02.28 00:00:00',  'YY.MM.DD HH24:MI:SS')                 AND (locations <> 'PUERTO RICO'                 OR locations <> 'JAPAN')                 AND CITY='LONDON' 

What I did

import cx_Oracle ip = 'XX.XX.X.XXX' port = YYYY SID = 'DW' dsn_tns = cx_Oracle.makedsn(ip, port, SID)  connection = cx_Oracle.connect('BA', 'PASSWORD', dsn_tns)  df_ora = pd.read_sql('SELECT* FROM TRANSACTION WHERE DIA_DAT>=to_date('15.02.28 00:00:00',  'YY.MM.DD HH24:MI:SS') AND (locations <> 'PUERTO RICO' OR locations <> 'JAPAN') AND CITY='LONDON'', con=connection)   

But I have this error

SyntaxError: invalid syntax 

What did I do wrong?

Thanks

like image 447
Kardu Avatar asked Mar 03 '16 19:03

Kardu


People also ask

Can I connect Oracle Database with Python?

By this command, you can install cx-Oracle package but it is required to install Oracle database first on your PC. connect(): Now Establish a connection between the Python program and Oracle database by using connect() function.

Can we use SQL in pandas DataFrame?

Pandasql is a python library that allows manipulation of a Pandas Dataframe using SQL. Under the hood, Pandasql creates an SQLite table from the Pandas Dataframe of interest and allow users to query from the SQLite table using SQL.

Which of the following utility of pandas can be used to read from Oracle Database?

Loading data from Oracle Database to pandas DataFramesread_sql() function in the above script. This function removes the burden of explicitly fetching the retrieved data and then converting it into the pandas DataFrame format. The read_sql() function does these tasks for you behind the scenes.


1 Answers

You need to properly quote your SQL Query. If you look at the syntax highlighting in your question (or an IDE), you'll notice that the single quotes aren't working as you expect.

Change the outer most quotes to double quotes - if you want it all on one line - or triple quotes if you want it across multiple lines:

query = """SELECT*             FROM TRANSACTION            WHERE DIA_DAT >=to_date('15.02.28 00:00:00',  'YY.MM.DD HH24:MI:SS')            AND (locations <> 'PUERTO RICO'            OR locations <> 'JAPAN')            AND CITY='LONDON'""" df_ora = pd.read_sql(query, con=connection) 
like image 83
Andy Avatar answered Sep 19 '22 07:09

Andy