我在更新数据库中的数据时遇到问题。我试图更新与程序标题位于同一行的每一行中的数据。
当我尝试这个时:
cur = con.cursor()
cur.execute('CREATE TABLE programs(channel TEXT, title TEXT, start_date TIMESTAMP, stop_date TIMESTAMP, button_id TEXT, description TEXT)')
con.commit()
for program in channel.findall('programme'):
cur.execute("INSERT INTO programs(channel, title, start_date, stop_date)" + " VALUES(?, ?, ?, ?)", [channel_name, title, start_time, stop_time])
programs_id = list()
for elem in program_button:
programs_id.append(elem.getId())
programs_id = map(str, programs_id)
#store the id of buttons in a database
for ids, title in zip(programs_id, prog_title):
cur.execute("UPDATE programs SET button_id = ? WHERE title=?", ([ids, title]))
con.commit()
cur.close()
它将更新与程序标题不在同一行的数据
#yields:
#channel | title | start_date | stop_date | button_id | description
###########################################################################
#BBC One | Tomorrow's Food | '2015-01-01'|'2016-01-01'| 3003 | blank
#BBC One | BBC News at Ten | '2015-01-01'|'2016-01-01'| 3056 | blank
#BBC One | South E Today | '2015-01-01'|'2016-01-01'| 3125 | blank
#BBC One | A Question of S | '2015-01-01'|'2016-01-01'| 3006 | blank
#BBC One | Film 2016 | '2015-01-01'|'2016-01-01'| 3007 | blank
应该是这样的:
#yields:
#channel | title | start_date | stop_date | button_id | description
###########################################################################
#BBC One | Tomorrow's Food | '2015-01-01'|'2016-01-01'| 3003 | blank
#BBC One | BBC News at Ten | '2015-01-01'|'2016-01-01'| 3004 | blank
#BBC One | South E Today | '2015-01-01'|'2016-01-01'| 3005 | blank
#BBC One | A Question of S | '2015-01-01'|'2016-01-01'| 3006 | blank
#BBC One | Film 2016 | '2015-01-01'|'2016-01-01'| 3007 | blank
我使用变量channel_name
,title
,start_time
和stop_time
将信息存储在数据库中,以便我可以用来从start_time和stop_time获取数据计算时间。当我没有添加对象列表时,我无法在插入行中使用button_id
,否则它将返回为空。
当我在更新行中的循环下使用print id时,以下是id列表:
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
以下是我在更新行中循环下使用print title
时的标题列表:
Tomorrow's Food
BBC News at Ten
South East Today
A Question of Sport
Film 2016
The Lookout
Weather for the Week Ahead
BBC News
Breakfast
Food: Truth or Scare
这是我想要实现的目标:
>>> find Tomorrow's Food in the title then store 3003 in the same row
>>> find BBC News at Ten in the title then store 3004 in the same row
>>> find South East Today in the title then store 3005 in the same row
>>> find A Question of Sport in the title then store 3006 in the same row
>>> find Film 2016 in the title then store 3007 in the same row
>>> find The Lookout in the title then store 3008 in the same row
>>> find Weather for the Week Ahead in the title then store 3009 in the same row
>>> find BBC News in the title then store 3010 in the same row
>>> find Breakfast in the title then store 3011 in the same row
>>> find Food: Truth or Scare in the title then store 3012 in the same row
那么我如何更新更新行中的button_id
以允许我将数据放在与标题相同的行的正确位置?
编辑:以下是数据库中的完整数据列表:
#yields:
#channel | title | start_date | stop_date | button_id | description
###########################################################################
#BBC One | Tomorrow's Food | '2015-01-01'|'2016-01-01'| 3003 | blank
#BBC One | BBC News at Ten | '2015-01-01'|'2016-01-01'| 3056 | blank
#BBC One | South E Today | '2015-01-01'|'2016-01-01'| 3125 | blank
#BBC One | A Question of S | '2015-01-01'|'2016-01-01'| 3006 | blank
#BBC One | Film 2016 | '2015-01-01'|'2016-01-01'| 3007 | blank
#BBC One | The Lookout | '2015-01-01'|'2016-01-01'| 3008 | blank
#BBC One | Weather for the | '2015-01-01'|'2016-01-01'| 3108 | blank
#BBC One | BBC News | '2015-01-01'|'2016-01-01'| 3240 | blank
#BBC One | Breakfast | '2015-01-01'|'2016-01-01'| 3110 | blank
#BBC One | Food: Truth or | '2015-01-01'|'2016-01-01'| 3234 | blank
#BBC One | Homes Under the | '2015-01-01'|'2016-01-01'| 3192 | blank
#BBC One | Wanted Down Under | '2015-01-01'|'2016-01-01'| 3235 | blank
#BBC One | Claimed and S | '2015-01-01'|'2016-01-01'| 3236 | blank
#BBC One | Bargain Hunt | '2015-01-01'|'2016-01-01'| 3248 | blank
#BBC One | BBC News at One | '2015-01-01'|'2016-01-01'| 3116 | blank
#BBC One | South East Today | '2015-01-01'|'2016-01-01'| 3125 | blank
#BBC One | Doctors | '2015-01-01'|'2016-01-01'| 3118 | blank
#BBC One | Moving On | '2015-01-01'|'2016-01-01'| 3119 | blank
#BBC One | Escape to the C | '2015-01-01'|'2016-01-01'| 3120 | blank
#BBC One | Holiday of My Lif | '2015-01-01'|'2016-01-01'| 3121 | blank
#BBC One | Flog It! | '2015-01-01'|'2016-01-01'| 3224 | blank
#BBC One | Pointless | '2015-01-01'|'2016-01-01'| 3123 | blank
#BBC One | BBC News at Six | '2015-01-01'|'2016-01-01'| 3124 | blank
#BBC One | South East Today | '2015-01-01'|'2016-01-01'| 3125 | blank
#BBC One | The One Show | '2015-01-01'|'2016-01-01'| 3126 | blank
#BBC One | EastEnders | '2015-01-01'|'2016-01-01'| 3128 | blank
#BBC One | Room 101 | '2015-01-01'|'2016-01-01'| 3029 | blank
#BBC One | EastEnders | '2015-01-01'|'2016-01-01'| 3128 | blank
#BBC One | Death in Paradise | '2015-01-01'|'2016-01-01'| 3031 | blank
以下是完整标题:
Tomorrow's Food
BBC News at Ten
South E Today
A Question of S
Film 2016
The Lookout
Weather for the
BBC News
Breakfast
Food: Truth or
Homes Under the
Wanted Down Under
Claimed and S
Bargain Hunt
BBC News at One
South East Today
Doctors
Moving On
Escape to the C
Holiday of My Life
Flog It!
Pointless
BBC News at Six
South East Today
The One Show
EastEnders
Room 101
EastEnders
Death in Paradise