Python CX_Oracle - 将数据从 Oracle 导入 Pandas 数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35781580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:57:22  来源:igfitidea点击:

CX_Oracle - import data from Oracle to Pandas dataframe

pythonoraclecx-oracle

提问by Kardu

Hy,

嗨,

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

我是 python 新手,我想使用这个简单的查询将一些数据从 Oracle 数据库导入 python(pandas 数据框)

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

谢谢

回答by Andy

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.

您需要正确引用您的 SQL 查询。如果您查看问题(或 IDE)中的语法突出显示,您会注意到单引号没有按预期工作。

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)