Python Forum

Full Version: Code import .CSV file to MySQL table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All ,

I am trying to load .csv file into MySQL table. i am getting error. Can anyone send me code or check below error any my code .
#!/usr/bin/python
import csv
import mysql.connector
# Establish a MySQL connection
mydb =mysql.connector.connect(host="localhost",user='root',password='mysql',database='test')
mycursor=mydb.cursor()

csv_data = csv.reader((file('Emp.csv'))
next(csv_data)
for row in csv_data:
mycursor.execute("INSERT INTO emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) VALUES(%s',%s,%s,%s,%s,%s,%s,%s)",row)
#close the connection to the database.
mydb.commit()
mycursor.close()

Error :

next(csv_data)
^
SyntaxError: invalid syntax

Process finished with exit code 1
Unmatched () in the line above.
csv_data = csv.reader(open('Emp.csv'))
for row in csv_data:
  mycursor.execute("INSERT INTO emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) 
Thanks for quick reply . Now i am getting this error.
 mycursor.execute("INSERT INTO emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) VALUES(%s',%s,%s,%s,%s,%s,%s,%s)",row)
^
IndentationError: expected an indented block

(Apr-30-2020, 02:48 PM)deanhystad Wrote: [ -> ]Unmatched () in the line above.

i am getting below error
mycursor.execute("INSERT INTO emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) VALUES(%s',%s,%s,%s,%s,%s,%s,%s)",row)
^
IndentationError: expected an indented block
Your code is not properly indented.

Check Section 2.1.8. Indentation in https://docs.python.org/3/reference/lexi...lysis.html