将数据插入sqlite3数据库

时间:2016-02-27 22:37:06

标签: python python-2.7 sqlite

我需要一些帮助,我创建了一个带有列的数据库,允许我将字符串列表存储在数据库中。现在我想在数据库中插入数据,但它只会将数据存储在按钮表中,而不是存储在程序表中。

当我尝试这个时:

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 and width of buttons in a database
for ids, width in zip(programs_id, program_width):
    cur.execute("INSERT INTO buttons(button_ids, button_width)" + " VALUES(?, ?)", [ids, width])
    cur.execute("INSERT INTO programs(button_id)" + " VALUES(?)", [ids])
con.commit()
cur.close()

我想在程序表的ids列中插入数据button_id,但是当我尝试时,没有任何事情发生。

你能帮我解释如何在程序表中插入数据吗?

编辑:当我尝试这个时:

for ids, width in zip(programs_id, program_width):
    cur.execute("UPDATE programs SET button_id = ? WHERE title=?", ([ids, title]))

此处它会将数据存储在错误的行中。

#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

1 个答案:

答案 0 :(得分:1)

根本没有插入程序表中吗?我认为使用当前插入程序表的方式,您将从第一个for循环中获得这样的行:

channel | title  | start_date | stop_date | button_id | description
###########################################################################
chanel1 | title1 | start1     | stop1     | blank     | blank

从第二个循环开始,你会有这样的行:

channel | title | start_date | stop_date | button_id | description
###########################################################################
blank   | blank | blank      | blank     | id1       | blank

我认为您可能需要在同一个SQL语句中插入button_id和channel info等,如下所示:

cur.execute("INSERT INTO programs(channel, title, start_date, stop_date, button_id)" + 
    " VALUES(?, ?, ?, ?)", [channel_name, title, start_time, stop_time, button_id])

或者你可以在第二个循环中用这样的更新语句更新字段:

cur.execute("UPDATE programs SET button_id = ? WHERE title=?",
    (button_id, title,) )

标题(或其他字段)是表的主键,或者首先插入button_id并完成此更新语句:

cur.execute("UPDATE programs SET channel=?, title=?, start_date=?, end_date = ? WHERE button_id = ?",
    (channel, title, start_date, end_date, button_id,) )

扩展评论的示例:

channel_name = 'PBS'
title = 'planet earth'
start_date = '2015-01-01'
end_date = '2016-01-01'

cur.execute("INSERT INTO programs(channel, title, start_date, stop_date)" + 
    " VALUES(?, ?, ?, ?)", [channel_name, title, start_time, stop_time] )

#yields:    
#channel | title        | start_date  | stop_date  | button_id | description
###########################################################################
#PBS     | planet earth | '2015-01-01'|'2016-01-01'| blank     | blank

button_id = '1'
title = 'planet earth'

cur.execute("UPDATE programs SET button_id = ? WHERE title=?",
    (button_id, title,) )

#yields:    
#channel | title        | start_date  | stop_date  | button_id | description
###########################################################################
#PBS     | planet earth | '2015-01-01'|'2016-01-01'| '1'       | blank

我认为现在的问题是你没有使用链接到你的程序的button_ids。也许使用program.getChannel等可以工作,这样你就可以同时插入button_id。否则从发布的代码我不知道如何将您的程序“链接”到按钮ID。 program_button中的elem是否具有get channel或get title属性?如果是这样,以上内容可能会变为:

cur.execute("UPDATE programs SET button_id = ? WHERE title=?",
    (button_id, elem.getTitle(),) )

或类似的东西。