-1
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?

0

2 Answers 2

1

The problem with your code is here:

for artists in artist_name:
    id_num = 0
    id_num += 1

Note how id_num gets reset on every iteration of the loop. That means that, for every entry in your list, id_num will always be 1 and triggers the ON CONFLICT clause of your query, leaving the first record untouched.

Instead, pull the counter outside of the loop:

id_num = 0
for artists in artist_name:
    id_num += 1

In addition, you should note that using string interpolation (f-strings) here is not a safe way to build queries. This is open to SQL Injection, which is a serious issue. Even though you're not exposed to outside data sources here, it's best to use parameterization from the start:

artist_name = ['Madonna', 'Slayer', 'Disturbed', 'Michael Jackson', 'Katty Parry']
with conn.cursor() as cur:
    id_num = 0
    for artists in artist_name:
        id_num += 1
        cur.execute(
            """
            INSERT INTO Artist (Id, Name) 
            VALUES (:id_num, :artists) 
            ON CONFLICT DO NOTHING
            """,
            {'id_num': id_num, 'artists': artists}
        )
Sign up to request clarification or add additional context in comments.

Comments

0

I'd try this:

artist_name = ['Madonna', 'Slayer', 'Disturbed', 'Michael Jackson', 'Katty Parry']
with conn.cursor() as cur:
    id_num = 0
    for artists in artist_name:
        id_num += 1
        cur.execute(f"""INSERT INTO Artist (Id, Name) 
                   VALUES ('{id_num}', '{artists}') 
                   ON CONFLICT DO NOTHING""");

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.

4 Comments

Why have you not fixed the SQL Injection vulnerability in the process? :'( Even from a set list, it's easier to do it properly all the time rather than string interpolation
One problem at a time. This is a new user and their first post. Let's try to fix the obvious problem and take it from there. You're welcome to post your answer and see if that's better. Why don't you do that instead of commenting on mine?
Because I thought that someone with 300k rep would try and show good practice in answers saved for posterity. SO is community moderated and I'm fully entitled to comment when I think something could be improved. You haven't even explained what you changed or why, so I'm not sure how you're exactly guiding the OP here.
You can comment. You choose not to answer. By all means, post something better and instruct me. It is obvious at a glance what's different. This is what I expect from someone with a 12K rep: not much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.