0

I have a list of data and need to insert each row back to MySQL. Below is an example of my data.

print(data[1:5])

[['Abilene, TX', 'Ahwatukee Foothills, AZ', 922.41258034], 
['Abilene, TX', 'Akron, OH', 1494.34762588], 
['Abilene, TX', 'Alafaya, FL', 1381.55219389], 
['Abilene, TX', 'Alameda, CA', 1680.9984747]]

My SQL columns are City, Destination, distance and so far I have:

for i in range(len(data)):
mycursor.execute("INSERT INTO table Shark (City, Destination, Distance) VALUES (:data)",[i])

which gives me an error "Not all parameters were used in the SQL Statment". How would I fix this?

1 Answer 1

1

try this:

data = [['Abilene, TX', 'Ahwatukee Foothills, AZ', 922.41258034], 
['Abilene, TX', 'Akron, OH', 1494.34762588], 
['Abilene, TX', 'Alafaya, FL', 1381.55219389], 
['Abilene, TX', 'Alameda, CA', 1680.9984747]]

for r in data:
    mycursor.execute("INSERT INTO table Shark (City, Destination, Distance) VALUES ({},{},{})".format(r[0], r[1], r[2])
Sign up to request clarification or add additional context in comments.

3 Comments

It's still giving me issues. I think it's because there are commas between the city and the state.
so I changed it to r=0 for r in data: print('INSERT INTO mileage (SourceCity, Destination, Distance) VALUES ({''''},{''''},{});'.format(data[r][0], data[r][1], data[r][2])) to pick up r
and it's giving me a list indices must be integers or slices, not list error

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.