SQL语句在SQL developer中执行,但在python(cx\U Oracle)中获取整个表
我试图使用cx_Oracle从Python执行sql语句。我编写的SQL似乎在我的Oracle数据库客户机(SQL Developer)中工作,但在从Python脚本执行时,似乎会收回所有记录SQL语句在SQL developer中执行,但在python(cx\U Oracle)中获取整个表,python,sql,oracle,oracle-sqldeveloper,cx-oracle,Python,Sql,Oracle,Oracle Sqldeveloper,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
#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()
这将返回视图中的每条记录,而不仅仅是像在SQL developer中那样返回包含“XXXX”的记录
有人能看到SQL中的问题或我执行此操作的方式中的问题吗
提前干杯据我所知,一些工具,如SQL*Plus,在SQL语句中有空行问题。不确定Python如何将您的语句传递给DB,但在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;