下面是数据库表的示例
date id name
01.02.11 4 aaaa
21.05.19 5 aaaa
31.12.12 5 aaaa
01.05.15 6 aaaa
为了以正确的方式查询数据(避免重复),在查询时,我必须将“报告日期”设置为第一个月的日期。
下面的代码为我提供了所需的结果,但只提供了一个月。
sql = 'select * from db where date = '01.03.20''
def oracle(user, pwd, dsn, sql, columns):
# Connection to databases
con = cx_Oracle.connect(user=user, password=pwd, dsn=dsn, encoding="UTF-8")
con.outputtypehandler = OutputHandler
# Cursor allows Python code to execute PostgreSQL command in a database session
cur = con.cursor()
# Check Connection
print('Connected')
# Create DF
df = pd.DataFrame(cur.execute(sql).fetchall(), columns= columns, dtype='object')[:]
print('Shape:', df.shape)
return df
问题:如何使用具有不同报告日期的CX_Oracle查询数据,而无需手动执行?
有多种方法可以直接使用SQL解决此问题。但是,预期的解决方案应使用“ a for循环”。
我正在考虑使用[更改报告日期
for i in [str(i).zfill(2) for i in range(1,13)]:
for j in [str(j).zfill(2) for j in range(0,21)]
sql = f'select * from db where date = '01.{i}.{j}''
想法是查询该日期的数据->将其存储在DF中
转到下个月01.02.19->将其存储在DF中
依此类推,直到达到范围21或到达上一个月(最新日期)
如果有人有想法使用cx_Oracle和Pandas使用循环来查询数据的不同日期,谢谢您的帮助!
from datetime import date, datetime, timedelta
import calendar
# Choose Start Month
start_month = date(2019, 1, 1)
# Get Current Month
current_month = date(datetime.today().year, datetime.today().month, 1)
# Create list to collect all successfully run queries
executed_sql_queries = []
# Create list for failed queries
failed_queries = []
# Create list to collect dfs
dfs = []
while start_month <= current_month:
query_date = start_month.strftime('%d.%m.%y')
sql = f"""select * from db where date = '{query_date}' """
try:
df = oracle(user, pwd, dsn, sql=sql, columns)
except sql_error as e:
print(e)
failed_queries.append(sql)
pass # move onto the next query or you can try re-running the query
else:
executed_sql_queries.append(sql)
dfs.append(df)
finally:
# Add one Month to the date for each run
days_in_month = calendar.monthrange(start_month.year, start_month.month)[1]
start_month = start_month + timedelta(days=days_in_month)
all_dfs = pd.concat(dfs)
executed_sql_queries:
["select * from db where date = '01.01.19' ", "select * from db where date = '01.02.19' ", "select * from db where date = '01.03.19' ", "select * from db where date = '01.04.19' ", "select * from db where date = '01.05.19' ", "select * from db where date = '01.06.19' ", "select * from db where date = '01.07.19' ", "select * from db where date = '01.08.19' ", "select * from db where date = '01.09.19' ", "select * from db where date = '01.10.19' ", "select * from db where date = '01.11.19' ", "select * from db where date = '01.12.19' ", "select * from db where date = '01.01.20' ", "select * from db where date = '01.02.20' ", "select * from db where date = '01.03.20' ", "select * from db where date = '01.04.20' "]