繁体   English   中英

SQL语句在sql developer中执行但是在python中获取整个表(cx_Oracle)

[英]SQL statement executes in sql developer but fetches entire table in python (cx_Oracle)

我正在尝试使用cx_Oracle从Python执行sql语句。 我写的SQL似乎在我的Oracle数据库客户端(SQL Developer)中工作,但似乎从Python脚本执行时撤回每条记录

#setup db connection here...
curs = db.cursor()

sql=("\
SELECT a.first, c.second, c.third, c.fourth FROM FIRST_DB.first_table a,\
FIRST_DB.second_table b,\
OTHER_DB.third_table c\
\
WHERE\
a.identifier='XXXX' and\
a.this_row = b.that_row and\
b.this_row = c.that_row;\
")

curs.execute(sql)
print curs.description
result=curs.fetchmany()
while result:
    for i,j,k,l in result:
        print i,j,k,l
    result=curs.fetchmany()
db.close()

这将返回视图中的每个记录,而不仅仅是包含'XXXX'的记录,就像它在SQL开发人员中一样...

任何人都可以看到SQL中的问题或我这样做的方式?

提前干杯

据我所知,像SQL * Plus这样的工具在sql语句中有空行问题。 不确定Python如何将您的语句传递给数据库,但可能会在where子句之前删除该空行将有助于:

#setup db connection here...
curs = db.cursor()

sql=("\
SELECT a.first, c.second, c.third, c.fourth FROM FIRST_DB.first_table a,\
FIRST_DB.second_table b,\
OTHER_DB.third_table c\
WHERE\
a.identifier='XXXX' and\
a.this_row = b.that_row and\
b.this_row = c.that_row;\
")

curs.execute(sql)
print curs.description
result=curs.fetchmany()
while result:
    for i,j,k,l in result:
        print i,j,k,l
    result=curs.fetchmany()
db.close()

尝试使用三引号字符串 这是一个可读的多行字符串的好方法。 我没有在这里测试Oracle DB,但我猜测缺少空白区域会导致问题。

打印您的SQL语句,您将看到删除反斜杠后的实际外观:

sql=("\
SELECT a.first, c.second, c.third, c.fourth FROM FIRST_DB.first_table a,\
FIRST_DB.second_table b,\
OTHER_DB.third_table c\
\
WHERE\
a.identifier='XXXX' and\
a.this_row = b.that_row and\
b.this_row = c.that_row;\
")

>>> print sql

SELECT a.first, c.second, c.third, c.fourth FROM FIRST_DB.first_table a,FIRST_DB
.second_table b,OTHER_DB.third_table cWHEREa.identifier='XXXX' anda.this_row = b
.that_row andb.this_row = c.that_row;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM