我可以从 Python 中很好地传递一个 sql 命令,但似乎无法将它与 SET 结合起来——我不想要标题,所以 SET HEADING OFF 是我正在尝试的。
这是显示我要去哪里的4次尝试:
1 - 获取日期,但也有错误消息:
session = Popen(['sqlplus','-S','user/pass@ORCL'], stdin=PIPE, stdout=PIPE, stderr=PIPE) sqlCommand = "SET HEADING OFF \n / \n select sysdate from dual;" session.communicate(sqlCommand) ('SP2-0103: Nothing in SQL buffer to run.\r\n\r\n09-JUL-13\r\n\r\n', '')
2 - 没有 SET 的命令:
session = Popen(['sqlplus','-S','user/pass@ORCL'], stdin=PIPE, stdout=PIPE, stderr=PIPE) sqlCommand = "select sysdate from dual;" session.communicate(sqlCommand) ('\r\nSYSDATE\r\n---------\r\n09-JUL-13\r\n\r\n', '')
3 - 尝试了单线:
session = Popen(['sqlplus','-S','user/pass@ORCL'], stdin=PIPE, stdout=PIPE, stderr=PIPE) sqlCommand = "set heading off \select sysdate from dual;" session.communicate(sqlCommand) ('SP2-0158: unknown SET option "\\select"\r\n', '')
4 - 试图分成两个电话:
session = Popen(['sqlplus','-S','user/pass@ORCL'], stdin=PIPE, stdout=PIPE, stderr=PIPE) sqlCommand = "select sysdate from dual;" session.stdin.write('SET HEADING OFF') session.communicate(sqlCommand) ('SP2-0265: heading must be set ON or OFF\r\n', '')
最后,如果这不是获取值的最简单方法(这是我的目标 - 只返回一个值 - 这不是最终查询),请告诉我。欢迎任何建议!