1

I am trying to complete a HW assignment where I have to pass values from a dictionary into a mySQL table with Python using the mySQL connector. To accomplish this, I turned the dictionary values into a string that I could pass into my INSERT query.

Here is the 'values string I pass into the command (just substitute values to see if the code works):

1, 1, 1, 1, 1, 111-111-1111, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1

For some reason every time I attempt to run this code I get these errors:

mysql.connector.errors.Datmysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1, 1, 1, 1, 1, 111-111-1111, 1, 1, 1, 1, 1, 1, 1, 1, 11, 1, 1, 1, 1, 1, 1, 1, 1,' at line 1

aError: 1136 (21S01): Column count doesn't match value count at row 1

import mysql.connector
db = mysql.connector.connect(user ='root', host = 'localhost', password ='Fiddlere2012!', db='py')
cursor = db.cursor() 
values = ', '.join((tuple(mds.values())))
print(values)    
insertMDS = ('INSERT INTO MDS' 
            '(manufacturer, location, contact, address, email, phone, modelNumber, moduleTotalLenxWid, moduleWeight, indCellArea, cellTech, cellManufacturer, cellManuLocation, totalCells, cellSeries, seriesStrings, bypassDiodes, bypassDiodeRating, bypassDiodeMaxTemp, seriesFuseRating, interconnectMatSupModNo, interconnectDims, superstrateType, superstrateMan, substrateType, substrateMan, frameType, frameAdhesive, encapsulantType, encapsulantMan, juncBoxType, juncBoxMan, juncBoxPottingMat, juncBoxAdhesive, juncBoxUse, cableConnectorType, maxSysVoltage, voc, isc, vmp, imp, pmp, ff)' 
            'VALUES ((%s))' % (values))

cursor.execute(insertMDS)

Please help me figure out what I am doing wrong if you can.. thank you!

2
  • ttake a look at thos answer stackoverflow.com/a/27649874/5193536 that is the way to insert data secure. no shortcuts Commented Oct 5, 2020 at 0:44
  • Thank you! this was super helpful. I am passing values into MySQL no problem now :) Commented Oct 5, 2020 at 20:28

0

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.