我正在从数据库中获取数据。我想搜索program_start_date
的第二天格式,例如:program_start_date
格式显示20171107200000
,因此我想搜索第二天的格式20171108200000
以下是代码:
channels_list = self.channel[self.channels_Index:min(len(self.channel), self.channels_Index + 7)]
if len(self.programs_button) > 0:
#CONNECT TO THE DATABASE
conn = database.connect(self.profilePath + 'source.db')
cur = conn.cursor()
for channels in channels_list:
cur.execute('SELECT channel, title, start_date, stop_date FROM programs where channel=? LIMIT 1', [channels])
programs = cur.fetchall()
for ind, row in enumerate(programs):
program_start_date = str(row[2])
start_time = time.strptime(program_start_date, '%Y%m%d%H%M%S')
start_time = datetime.datetime.fromtimestamp(time.mktime(start_time))
print program_start_date
以下是program_start_date
数据:
20171107200000
20171107210000
20171107220000
20171107223000
20171107224500
20171107232500
20171108000500
20171108001000
20171108060000
20171108091500
20171108100000
20171108110000
20171108114500
20171108121500
20171108130000
20171108133000
20171108134500
20171108141500
20171108150000
20171108154500
20171108163000
20171108171500
20171108180000
20171108183000
20171108190000
20171108200000
20171108210000
20171108220000
20171108223000
20171108224500
20171108231500
20171108234500
20171109004500
20171109005000
20171109060000
20171109091500
20171109100000
20171109110000
20171109114500
20171109121500
20171109130000
20171109133000
20171109134500
20171109141500
20171109150000
20171109154500
20171109163000
20171109171500
20171109180000
20171109183000
20171109190000
20171109193000
20171109200000
20171109210000
20171109220000
20171109223000
20171109224500
20171109234500
20171110004500
20171110005000
20171110060000
20171110091500
20171110100000
20171110110000
20171110114500
20171110121500
20171110130000
20171110133000
20171110134500
20171110141500
20171110150000
20171110154500
20171110163000
20171110171500
20171110180000
20171110183000
20171110190000
20171110193000
20171110200000
20171110203000
20171110210000
20171110213000
这是start_time
数据:
2017-11-07 20:00:00
2017-11-07 21:00:00
2017-11-07 22:00:00
2017-11-07 22:30:00
2017-11-07 22:45:00
2017-11-07 23:25:00
2017-11-08 00:05:00
2017-11-08 00:10:00
2017-11-08 06:00:00
2017-11-08 09:15:00
2017-11-08 10:00:00
2017-11-08 11:00:00
2017-11-08 11:45:00
2017-11-08 12:15:00
2017-11-08 13:00:00
2017-11-08 13:30:00
2017-11-08 13:45:00
2017-11-08 14:15:00
2017-11-08 15:00:00
2017-11-08 15:45:00
2017-11-08 16:30:00
2017-11-08 17:15:00
2017-11-08 18:00:00
2017-11-08 18:30:00
2017-11-08 19:00:00
2017-11-08 20:00:00
2017-11-08 21:00:00
2017-11-08 22:00:00
2017-11-08 22:30:00
2017-11-08 22:45:00
2017-11-08 23:15:00
2017-11-08 23:45:00
2017-11-09 00:45:00
2017-11-09 00:50:00
2017-11-09 06:00:00
2017-11-09 09:15:00
2017-11-09 10:00:00
2017-11-09 11:00:00
2017-11-09 11:45:00
2017-11-09 12:15:00
2017-11-09 13:00:00
2017-11-09 13:30:00
2017-11-09 13:45:00
2017-11-09 14:15:00
2017-11-09 15:00:00
2017-11-09 15:45:00
2017-11-09 16:30:00
2017-11-09 17:15:00
2017-11-09 18:00:00
2017-11-09 18:30:00
2017-11-09 19:00:00
2017-11-09 19:30:00
2017-11-09 20:00:00
2017-11-09 21:00:00
2017-11-09 22:00:00
2017-11-09 22:30:00
2017-11-09 22:45:00
2017-11-09 23:45:00
2017-11-10 00:45:00
2017-11-10 00:50:00
2017-11-10 06:00:00
2017-11-10 09:15:00
2017-11-10 10:00:00
2017-11-10 11:00:00
2017-11-10 11:45:00
2017-11-10 12:15:00
2017-11-10 13:00:00
2017-11-10 13:30:00
2017-11-10 13:45:00
2017-11-10 14:15:00
2017-11-10 15:00:00
2017-11-10 15:45:00
2017-11-10 16:30:00
2017-11-10 17:15:00
2017-11-10 18:00:00
2017-11-10 18:30:00
2017-11-10 19:00:00
2017-11-10 19:30:00
2017-11-10 20:00:00
2017-11-10 20:30:00
2017-11-10 21:00:00
2017-11-10 21:30:00
在数据库中,我有249行program_start_date
所以如何使用program_start_date
变量在数据库中搜索第二天的格式?
答案 0 :(得分:1)
import datetime as dt, time
TIME_FORMAT = '%Y%m%d%H%M%S'
program_start_date = '20171108150000'
start_time = dt.datetime.fromtimestamp(time.mktime(time.strptime(program_start_date, TIME_FORMAT)))
nextday = start_time + dt.timedelta(days=1)
program_nextday_date = nextday.strftime(TIME_FORMAT)
print program_nextday_date
20171109150000
使它成为一个功能:
def get_next_day(time_string):
to_datetime = dt.datetime.fromtimestamp(time.mktime(time.strptime(time_string, TIME_FORMAT)))
nextday = to_datetime + dt.timedelta(days=1)
return nextday.strftime(TIME_FORMAT)
要查找某一天第二天的所有日期,请使用:
nextday = get_next_day(program_start_date)
nextnextday = get_next_day(nextday)
curr.execute('SELECT * FROM table_name WHERE start_date >= ? and stop_date < ?', [nextday, nextnextday])