将%s
替换为所需的开始时间后,代码的SQL部分在我的SQL中起作用。我已经尝试了一切,似乎无法在python中获得结果-返回值为[]
。
我尝试重写WHERE from (Convert_TZ(Tablex, 'TZ1', 'TZ2') Between 'Time1' and 'time2'
,因为之前我还没有看到查询在python之间使用。
import mysql.connector
import xlsxwriter
import datetime
while True:
try:
LB = input('Enter Lower Bound date/time in YYMMDD hhmm >>> ')
YY = '20' + LB[:2]
MM = LB[2:4]
DD = LB[4:6]
hh = LB[7:9]
mm = LB[9:]
LB_date = datetime.datetime(int(YY), int(MM), int(DD), int(hh), int(mm))
print(LB_date)
break
except ValueError:
print('Invalid Entry')
print('Date/Time must be in YYMMDD HHMM format')
print('\n')
while True:
try:
UB = input('Enter Upper Bound date/time in YYMMDD hhmm >>> ')
if UB.upper() == 'NOW':
UB_date = UB.upper()
YY = '20' + LB[:2]
MM = LB[2:4]
DD = LB[4:6]
hh = LB[7:9]
mm = LB[9:]
UB_date = datetime.datetime(int(YY), int(MM), int(DD), int(hh), int(mm))
break
except ValueError:
print('Invalid Entry')
print('Date/Time must be in YYMMDD HHMM format')
print('\n')
us_cnx = mysql.connector.connect(user='user',
password='password',
host='host',
port=1234,
database='schema')
us_cursor = us_cnx.cursor(buffered=True)
if UB_date == 'NOW':
us_query = ("""
SELECT
distinct(parent.name),
CONVERT_TZ(parent.created, 'GMT', 'US/Pacific'),
nc.description,
nc.createdby,
thingdata.valuetext,
actor.name,
nca.disposition,
nca.description,
nc.closedby,
convert_tz(nc.closed, 'GMT', 'US/Pacific')
FROM
thing child
LEFT JOIN
thing parent ON parent.id = child.parentid
LEFT JOIN
nc ON nc.thingid = parent.id
LEFT JOIN
thingdata ON thingdata.thingid = parent.id
JOIN
actor ON parent.actorcreatedby = actor.id
left join
ncaction nca on nca.ncid = nc.id
WHERE
parent.actorcreatedby IN (
'2624519',
'2624520',
'2624521',
'2624522',
'2624523',
'2624524',
'2624525',
'2624526',
'2624527' )
AND nc.createdby like 'actor'
AND nc.description != 'NULL'
AND parent.created > CONVERT_TZ(%s, 'US/Pacific', 'GMT')
AND parent.created < convert_TZ(NOW(), 'US/Pacific', 'GMT')
AND thingdata.taskid IN ('1616757')
""")
us_cursor.execute(us_query, (LB_date,))
else:
us_query = ("""
SELECT
distinct(parent.name),
CONVERT_TZ(parent.created, 'GMT', 'US/Pacific'),
nc.description,
nc.createdby,
thingdata.valuetext,
actor.name,
nca.disposition,
nca.description,
nc.closedby,
convert_tz(nc.closed, 'GMT', 'US/Pacific')
FROM
thing child
LEFT JOIN
thing parent ON parent.id = child.parentid
LEFT JOIN
nc ON nc.thingid = parent.id
LEFT JOIN
thingdata ON thingdata.thingid = parent.id
JOIN
actor ON parent.actorcreatedby = actor.id
left join
ncaction nca on nca.ncid = nc.id
WHERE
parent.actorcreatedby IN (
'2624519',
'2624520',
'2624521',
'2624522',
'2624523',
'2624524',
'2624525',
'2624526',
'2624527' )
AND nc.createdby like 'ignition-gf1-bm-tag4-prod'
AND nc.description != 'NULL'
AND parent.created > CONVERT_TZ(%s, 'US/Pacific', 'GMT')
AND parent.created < convert_TZ(%s, 'US/Pacific', 'GMT')
AND thingdata.taskid IN ('1616757')
""")
us_cursor.execute(us_query, (LB_date, UB_date))
us_results = us_cursor.fetchall()
us_cursor.close()
us_cnx.close()
for i in range(len(us_results)):
us_results.append((us_results[i][0], us_results[i][1], us_results[i][2], us_results[i][3],
us_results[i][4], us_results[i][5], us_results[i][6], us_results[i][7],
us_results[i][8], us_results[i][9]))
print(us_results)
workbook = xlsxwriter.Workbook('U-S.xlsx')
time_format = workbook.add_format({'num_format': 'yyyy-mm-dd hh:mm:ss'})
counter_format = workbook.add_format({'num_format': 'hh:mm:ss'})
worksheet = workbook.add_worksheet()
Headers = ('Serial', 'Created PST', 'Defect', 'NC Created By', 'US ID', 'Created By',
'Disposition', 'Rework Notes', 'NC Closed By', 'NC Closed PST')
for i in range(len(Headers)):
worksheet.write(0, i, Headers[i])
xlsx_row = 1
for row in us_results:
for j in range(len(Headers)):
worksheet.add_table(j, {'style': 'Table Style Dark 11'})
worksheet.set_column(j, 32)
if j in [1, 9]:
worksheet.write(xlsx_row, j, row[j], time_format)
else:
worksheet.write(xlsx_row, j, row[j])
xlsx_row += 1
workbook.close()
一切正常,但US_results
中什么也没有返回(只是在控制台中以[]
打印),并且excel文件中只有标头。