Python Forum
Code import .CSV file to MySQL table - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Code import .CSV file to MySQL table (/thread-26402.html)



Code import .CSV file to MySQL table - rtakle - Apr-30-2020

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


RE: Code import .CSV file to MySQL table - deanhystad - Apr-30-2020

Unmatched () in the line above.


RE: Code import .CSV file to MySQL table - anbu23 - Apr-30-2020

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) 



RE: Code import .CSV file to MySQL table - rtakle - Apr-30-2020

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


RE: Code import .CSV file to MySQL table - anbu23 - Apr-30-2020

Your code is not properly indented.

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