gpt4 book ai didi

python - 在数据库中搜索第二天格式

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:44 26 4
gpt4 key购买 nike

我正在努力从数据库中获取数据。我想搜索 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 变量在数据库中搜索第二天的格式?

最佳答案

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])

关于python - 在数据库中搜索第二天格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47166563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com