0
with open('employee.txt') as textfile:
    csvreader = csv.reader(textfile, delimiter='\t')
    csvdata = []
    for row in csvreader:
        csvdata.append(row)

conn = pymysql.connect (host = "localhost",user = "root", passwd = "password",db = "details1")
c = conn.cursor()
for row in csvdata:
    c.execute("INSERT INTO employee6(serial_no, first_name, last_name, age, sex,city) VALUES ('%s', '%s', '%s', '%s', '%s','%s')" % (serial_no,first_name,last_name,age,sex,city))

conn.commit()
c.close()`enter code here`

but I am getting an error in () 8 c = conn.cursor() 9 for row in csvdata: ---> 10 c.execute("INSERT INTO employee6(serial_no, first_name, last_name, age, sex,city) VALUES ('%s', '%s', '%s', '%s', '%s','%s')" % (serial_no,first_name,last_name,age,sex,city)) 11 12 conn.commit() NameError: name 'serial_no' is not defined why am i getting this error

5
  • it seems that serial_no does not exist in your code. Did you define it? Commented Aug 9, 2018 at 11:19
  • I have serial_no in table i created and even in my text file Commented Aug 9, 2018 at 11:25
  • print(row) please Commented Aug 9, 2018 at 11:44
  • its printing my text file with extra white spaces Commented Aug 9, 2018 at 13:07
  • @cherry please show us at least one or two rows of the output of print(row) if possible. ANd that would the the rowabouve your c.execute() line. That's kind of relevant for the answer. Commented Aug 9, 2018 at 13:10

2 Answers 2

1

It should be:

        c.execute("INSERT INTO employee6(serial_no, first_name, last_name, age, sex,city) VALUES ('%s', '%s', '%s', '%s', '%s','%s')" % (row["serial_no"], row["first_name"], row["last_name"], row["age"],row["sex"],row["city"]))

Edited

    with open('employee.txt') as textfile:
        csvreader = csv.reader(textfile, delimiter='\t')
        csvdata = []
        conn = pymysql.connect (host = "localhost",user = "root", passwd = "password",db = "details1")
        c = conn.cursor()
        for row in csvreader:
            c.execute("INSERT INTO employee6(serial_no, first_name, last_name, age, sex,city) VALUES ('%s', '%s', '%s', '%s', '%s','%s')" % (row["serial_no"], row["first_name"], row["last_name"], row["age"],row["sex"],row["city"]))


        conn.commit()
        c.close()
Sign up to request clarification or add additional context in comments.

8 Comments

its throwing me an error saying" list indices must be integers or slices, not str"
No, sorry, but no, it should not be that :) You are proposing the use of string interpolation, which not only makes the code vulnerable to SQL injection attacks, but also will break if, for example, the serial_no is a numeric data type in the DB where the above would attempt to insert a string.
Show output of print(rows) please @cherry
Above code is to make sure that we are inputting correct value in DB @shmee
['Serial_no', 'FIRST_NAME', 'LAST_NAME', 'AGE', 'SEX', 'city', ''] ['1', '', 'john', '', 'mathew', '', '32', 'm', 'bombay'] ['2', '', 'jim', '', 'parker', '', '35', 'm', 'pune'] ['3', '', 'sophia', '', 'ran', '', '28', 'f', 'kolkata'] ['4', '', 'wendi', '', 'blake', '', '40', 'f', 'chennai'] @upasana
|
0

Assuming that reading the input file works correctly, you will have a list of list in your csvdata variable.

The way you try to create your SQL query string by using string interpolation is not recommended and potentially dangerous. It allows for SQL injection attacks, but also can make formatting the string to enforce the correct data types, especially for INSERT queries a pain.

All DB-API 2-compliant adaptors alow to pass a sequence of parameters along with your query string that the adaptor will safely replace.

Since row should already be a list when you iterate over csvdata, your execute call would look like this:

for row in csvdata:
    c.execute("INSERT INTO employee6(serial_no, first_name, last_name, age, sex,city) VALUES (%s, %s, %s, %s, %s,%s)", row)

However, since your csvdata is already a list of lists, you could also use executemany instead of the above:

c.executemany("INSERT INTO employee6(serial_no, first_name, last_name, age, sex,city) VALUES (%s, %s, %s, %s, %s,%s)", csvdata[1:])

Note: the csvdata[1:] is to exclude the header line from the INSERT.

6 Comments

its showing "not all arguments converted during string formatting"@shmee
Is there any row in your input file where any of the fields is missing or where there is an additional field? Which option did you use? The execute() or executemany()? If the execute(), print row before the insert to identify the potentially offending row.
I dont have any fields missing and i used execute many and print(row) before for loop and insert statement
print(csvdata[:-1] what does that return?
[['Serial_no', 'FIRST_NAME', 'LAST_NAME', 'AGE', 'SEX', 'city', ''], ['1', '', 'john', '', 'mathew', '', '32', 'm', 'bombay'], ['2', '', 'jim', '', 'parker', '', '35', 'm', 'pune'], ['3', '', 'sophia', '', 'ran', '', '28', 'f', 'kolkata'], ['4', '', 'wendi', '', 'blake', '', '40', 'f', 'chennai']] and its repeating@shmee
|

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.