30

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

1

1 Answer 1

57

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)
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.