I have a question. When I execute the whole thing, it throws me an error stating: TypeError: not enough arguments for format string. I guess it has to do with the error in the cur.execute("INSERT INTO modules
(module_name, no_of_scripts, gsm, exam_type, exam_date, school) VALUES (%s, %s, %s, %s, %s, %s)") but unsure what could be causing the issue.Help please thanks!
Below is a reference code:
db = MySQLdb.connect(host='localhost',
user="schooluser",
passwd="",
db="exam_import")
cur = db.cursor()
csv_data = csv.reader(file('Modules.csv'))
for row in csv_data:
cur.execute("INSERT INTO `modules`(module_name, no_of_scripts, gsm, exam_type, exam_date, school) VALUES (%s, %s, %s, %s, %s, %s)")
cur.execute("SELECT * FROM modules")
for row in cur.fetchall():
self.name = row[0]
self.sheetsperpaper = row[1]
self.paperweightlol = row[2]
self.exam_type1 = row[3]
self.exam_series1 = row[4]
self.exam_school1 = row[5]
self.module_name.append(self.name)
self.sheets.append(self.sheetsperpaper)
self.paperweight.append(self.paperweightlol)
self.exam_type.append(self.exam_type1)
self.exam_series.append(self.exam_series1)
self.exam_school.append(self.exam_school1)
db.commit()
db.close()
You need to asign values to your query
cur.execute("INSERT INTO `modules`(module_name, no_of_scripts, gsm, exam_type, exam_date, school) VALUES (%s, %s, %s, %s, %s, %s)" % (row[0],int(row[1]),int(row[2]),row[3],row[4],row[5]))
so you asign the values of each column of the row to your query (if the order is not that, you can change the row[x] position)
I got the same error once. I found this thing somewhere that when inserting a char or varchar value into the database in python code use single quotes around '%s'
. I guess this should work for you too.
"insert into table_name(column_name) values('%s')" % value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.