skip to Main Content

I am using phpmyadmin for mysql databse.

import csv
import MySQLdb
import pymysql

db = pymysql.connect(
    host="127.0.0.1",
        port=3308,
        user="root",
        passwd="",
        db="database_name"
 )
cursor = db.cursor()

csv_data = csv.reader(r'E:py_docsfile101.csv')
next(csv_data)
for row in csv_data:

    cursor.execute('INSERT INTO table_name(A, B, C, D, E, F, G, H, I, J, K, L, M )' 'VALUES("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")', row)
db.commit()
cursor.close()
print("Done")

The error message I am getting is

query = query % self._escape_args(args, conn)
TypeError: not enough arguments for format string

I am using pymysql. Is there any other way to import csv to Mysql using python ?

2

Answers


  1. The format string (query) contains more occurrences of %s than you have supplied arguments in "row" from the csv file. Are all the lines the same number of fields?

    I’ve always had more luck with named parameters when doing sql. This bit of code grabs the headers from the first line of the csv, then creates a list of dictionaries that use those headers from the rest of the file. This allows us to call executemany(query_string, list) and use named place holders in the query.

    Using named place holders, the order and quantity of parameters is not such a big deal. As long as you have a field in the dictionary for each place holder, everything will sort itself out as far as ordering goes. You can also have extra fields in the dictionary that you don’t use in the query without it blowing up. These properties make it easier to make changes to both the query and the code and also make your sql much more readable.

    Using executemany can be more efficient if the driver supports prepared queries.

    import csv
    import pymysql
    
    data = [] 
    headers = True
    with open('E:py_docsfile101.csv') as f:
        for row in csv.reader(f):
            if headers:
                fields = [field for field in row]
                headers = False
            else:
                data.append({field: val for field, val in zip(fields, row)})
    with pymysql.connect(host="127.0.0.1", port=3308, user="root", passwd="", db="database_name").cursor() as cur:
        cur.executemany("insert into table_name (field_1, field_2, field_3, ...) "
            "values (%(csv_fld_1)s, %(csv_fld_2)s, %(csv_fld_3)s, ...) "
            ,data)
        cur.connection.commit()```
    
    Login or Signup to reply.
  2. You are getting this error because your CSV "row" does not have 13 columns always. The better way to do it insert empty string ("") or null in csv rows for the columns where you don’t have values.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search