Home > front end >  Python : "Not enough arguments" error when insert into MySQL
Python : "Not enough arguments" error when insert into MySQL

Time:12-14

I'm trying to insert multiple .csv files into an existing MySQL database.

Here's my code:

import pandas as pd
import os
import glob
import csv
import MySQLdb


source = "D:\x\x\wetransfer"
dest = 'D:\x\x\wetransfercsv'
os.chdir(dest)



#insert les csv dans la bdd
mydb = MySQLdb.connect(host='localhost',
       user='root',
       passwd='',
       db='oalley')
cursor = mydb.cursor()

for file in glob.glob("*.csv"):
       data = pd.read_csv(file)
       df = pd.DataFrame(data)
       print(df)
       
       for row in csv.reader(file):
              cursor.execute("INSERT INTO entreprise (siren, siret, denomination, enseigne1etablissement, datecreationetablissement, trancheeffectifsetablissement, adresseetablissement, codepostaletablissement, activiteprincipaleetablissement, denominationunitelegale, nombreetablissementunitelegale, caunitelegale, dateclotureexercice, phone, website, representants, coordonnees)" 
                     "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                     row)



mydb.commit()
cursor.close()

The problem is, when i execute the code, i get this error : MySQLdb._exceptions.ProgrammingError: not enough arguments for format string

I've counted right and i have the same number of '%s' and columns so i don't know why this error is popping out.

Thanks in advance for the help, i'm starting to go crazy, I can't found anything lol

CodePudding user response:

Your code is like this:

cursor.execute("INSERT..." 
    "VALUES...",
    row)

This looks strange. You have two lines in your SQL statement as separate strings. execute() expects the second argument to be a list, not a second string.

Try formatting multi-line strings like this:

cursor.execute("""INSERT... 
    VALUES...""",
    row)
  • Related