Вопрос:

Update the data in the same rows as the other data

python python-2.7

20 просмотра

1 ответ

58 Репутация автора

I need some help with my code. I want to find the data of program_start_date into the database as I would like to store the program_id in the same rows as the program_start_date data.

When I try this:

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

It will not let me to update the program_id in the same rows as the start_date data.

Here is the full code:

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

Here is the program_start_date data:

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
Автор: Danny White Источник Размещён: 08.11.2017 11:28

Ответы (1)


1 плюс

14922 Репутация автора

Решение

Your update is using the wrong values. Should be:

cur.execute("UPDATE programs SET program_id=? WHERE start_date=?",(program_id, program_start_date))
conn.commit()
Автор: smac89 Размещён: 08.11.2017 11:39
Вопросы из категории :
32x32