artist_name = ['Madonna', 'Slayer', 'Disturbed', 'Michael Jackson', 'Katty Parry']
with conn.cursor() as cur:
for artists in artist_name:
id_num = 0
id_num += 1
cur.execute(f"""INSERT INTO Artist (Id, Name)
VALUES ('{id_num}', '{artists}')
ON CONFLICT DO NOTHING""");
The loop adds only the first element of the list to the database, assigning it id = 1. How to add the entire list to the database?
I'd try this:
Let me spell out the difference for you: I moved the initialization of
id_num
outside the loop.Some responders feel like SQL injection is key here. I agree that it's important, but your first problem is to solve your INSERT issue. Once you've done so, perhaps you could read this to understand the problem better.