帮我编写一个python脚本,先由用户输入查询日期,然后连接oracle服务器,查询语句为 from WMS_B_SN_H t left join WMS_B_TASK_HIS q on t.container_id = q.container_id where ( q.ref_doc_type = 'DocType_Replenish' or q.ref_doc_type = 'DocType_Retrieval' ) and q.create_date between to_date('2023/05/17 06:58:28', 'yyyy-mm-dd hh24:mi:ss') and to_date('2023/05/17 23:58:28', 'yyyy-mm-dd hh24:mi:ss') union select * from Wms_b_Sn_h_His t left join WMS_B_TASK_HIS q on t.container_id = q.container_id where ( q.ref_doc_type = 'DocType_Replenish' or q.ref_doc_type = 'DocType_Retrieval' ) and q.create_date between to_date('2023/05/17 06:58:28', 'yyyy-mm-dd hh24:mi:ss') and to_date('2023/05/17 23:58:28', 'yyyy-mm-dd hh24:mi:ss');,把其中的日期改为用户输入的查询日期,查询后将结果保存在脚本所在目录的“查询日期”+WMS出库记录.xlsx表格中
时间: 2024-02-10 08:10:57 浏览: 20
好的,以下是Python脚本代码:
```python
import cx_Oracle
import pandas as pd
import datetime
# 用户输入查询日期
search_date = input("请输入查询日期(格式为yyyy-mm-dd):")
# 连接Oracle服务器
dsn = cx_Oracle.makedsn("服务器地址", "端口号", "服务名")
conn = cx_Oracle.connect("用户名", "密码", dsn)
# 构建查询语句
sql = f"""select *
from (
select *
from WMS_B_SN_H t
left join WMS_B_TASK_HIS q on t.container_id = q.container_id
where (q.ref_doc_type = 'DocType_Replenish' or q.ref_doc_type = 'DocType_Retrieval')
and q.create_date between to_date('{search_date} 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and to_date('{search_date} 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
union
select *
from Wms_b_Sn_h_His t
left join WMS_B_TASK_HIS q on t.container_id = q.container_id
where (q.ref_doc_type = 'DocType_Replenish' or q.ref_doc_type = 'DocType_Retrieval')
and q.create_date between to_date('{search_date} 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and to_date('{search_date} 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
)"""
# 查询数据
df = pd.read_sql_query(sql, conn)
# 关闭数据库连接
conn.close()
# 保存查询结果
filename = f"{search_date} WMS出库记录.xlsx"
df.to_excel(filename, index=False)
print(f"查询结果已保存在当前目录下的 {filename} 文件中。")
```
请将代码中的“服务器地址”、“端口号”、“服务名”、“用户名”和“密码”替换为实际的数据库连接信息。另外,由于我无法连接到您的Oracle服务器,所以无法进行测试,如果出现任何问题,请及时联系我。