更新与其他数据相同的行中的数据

时间:2017-11-08 23:28:24

标签: python python-2.7

我的代码需要一些帮助。我想在数据库中找到program_start_date的数据,因为我希望将program_id存储在与program_start_date数据相同的行中。

当我尝试这个时:

cur.execute("UPDATE programs SET program_id=? WHERE start_date=?",(program_start_date, program_id))
conn.commit()

它不允许我在与start_date数据相同的行中更新program_id。

以下是完整代码:

nextday_list = ['20171109224500', '20171109223000', '20171109224500', '20171109223500', '20171109220000', '20171109224000', '20171109230000']
program_id_list = ['3001', '3002', '3003', '3004', '3005', '3006', '3007', '3008', '3009', '3010', '3011', '3012', '3013', '3014', '3015', '3016', '3017', '3018', '3019', '3020', '3021', '3022', '3023', '3024', '3025', '3026', '3027', '3028', '3029', '3030', '3031', '3032', '3033', '3034', '3035', '3036', '3037', '3038', '3039', '3040', '3041', '3042', '3043', '3044', '3045', '3046', '3047', '3048', '3049', '3050', '3051', '3052', '3053', '3054', '3055', '3056', '3057', '3058', '3059', '3060', '3061', '3062', '3063', '3064', '3065', '3066', '3067', '3068', '3069', '3070']

for nextday, program_id in zip(nextday_list, program_id_list):
    cur.execute('SELECT start_date, program_id FROM programs WHERE start_date >= ? LIMIT 10', [nextday,])
    programs = cur.fetchall()

    for ind, row in enumerate(programs):
        program_start_date = str(row[0])
        cur.execute("UPDATE programs SET program_id=? WHERE start_date=?",(program_start_date, program_id))
        conn.commit()

以下是program_start_date数据:

20171109224500
20171109234500
20171110004500
20171110005000
20171110060000
20171110091500
20171110100000
20171110110000
20171110114500
20171110121500
20171109223000
20171109224500
20171109234500
20171110004500
20171110005000
20171110060000
20171110091500
20171110100000
20171110110000
20171110114500
20171109224500
20171109234500
20171110004500
20171110005000
20171110060000
20171110091500
20171110100000
20171110110000
20171110114500
20171110121500
20171109224500
20171109234500
20171110004500
20171110005000
20171110060000
20171110091500
20171110100000
20171110110000
20171110114500
20171110121500
20171109220000
20171109223000
20171109224500
20171109234500
20171110004500
20171110005000
20171110060000
20171110091500
20171110100000
20171110110000
20171109224500
20171109234500
20171110004500
20171110005000
20171110060000
20171110091500
20171110100000
20171110110000
20171110114500
20171110121500
20171109234500
20171110004500
20171110005000
20171110060000
20171110091500
20171110100000
20171110110000
20171110114500
20171110121500
20171110130000

1 个答案:

答案 0 :(得分:1)

您的更新使用了错误的值。应该是:

cur.execute("UPDATE programs SET program_id=? WHERE start_date=?",(program_id, program_start_date))
conn.commit()