Sqlite:从表中更新多个列

时间:2014-06-25 09:12:20

标签: python sqlite

问题

我使用python导入了一个csv并创建了一个名为upload的表。我想用新的上传表值更新Fixture表。我不想插入,因为我有每个夹具的特定ID,我不想搞砸插入。下面是我的代码,它适用于insert但不适用于更新。有什么想法吗?

Sqlite代码

cursor = conn.execute("select * from upload")

for row in cursor:
    cur.execute("update Fixture \
                set home_score = ?\
                    ,away_score = ?\
                WHERE home_team in ?\
                and away_team in ?",(row[1],row[2],row[0],row[3]))
    conn.commit()
print "update fixture done";

灯具表

(1, u'2014-06-12', 1, u'Group A', u'Brazil', None, u'Croatia', 1, None, None, None, u'21:00 (ITV)')                                                                       
(2, u'2014-06-13', 11, u'Group A', u'Mexico', None, u'Cameroon', None, None, None, None, u'17:00 (ITV)')                                                                     
(3, u'2014-06-17', 5, u'Group A', u'Brazil', None, u'Mexico', None, None, None, None, u'20:00 (BBC)')                                                                        
(4, u'2014-06-18', 10, u'Group A', u'Cameroon', None, u'Croatia', None, None, None, None, u'23:00 (ITV)')                                                                    
(5, u'2014-06-23', 3, u'Group A', u'Cameroon', None, u'Brazil', None, None, None, None, u'21:00 (ITV)')                                                                      
(6, u'2014-06-23', 9, u'Group A', u'Croatia', None, u'Mexico', None, None, None, None, u'21:00 (ITV)') 

更新数据

u'Cameroon', 1,4, u'Brazil'                                                                   
u'Croatia', 1,3, u'Mexico'

测试

cursor = conn.execute("select * from upload")
for row in cursor:
    print row[1],row[2],row[0],row[3]

测试输出

2 1 Greece Ivory Coast                                                                                                                                                 
1 4 Japan Colombia                                                                                                                                                     
0 0 Costa Rica England                                                                                                                                                 
0 1 Italy Uruguay 

1 个答案:

答案 0 :(得分:1)

你错过了cursor.fetchall()

for row in cursor.fetchall():
    stmt='''update Fixture set home_score = :home_score,away_score = :away_score WHERE home_team = :home_team and away_team =:away_team''' 
    cur.execute(stmt,dict(home_score =row[1],away_score=row[2],home_team=row[0],away_team =row[3]))
conn.commit()
print "update fixture done";