Python Forum
Code import .CSV file to MySQL table
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code import .CSV file to MySQL table
#1
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
Reply
#2
Unmatched () in the line above.
Reply
#3
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) 
Reply
#4
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
Reply
#5
Your code is not properly indented.

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 607 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,147 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,665 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Mysql Workbench table not updated CatBall 2 1,044 Feb-13-2023, 05:37 PM
Last Post: CatBall
  Import XML file directly into Excel spreadsheet demdej 0 798 Jan-24-2023, 02:48 PM
Last Post: demdej
Photo How to select NULL and blank values from MySQL table into csv python300 9 2,328 Dec-27-2022, 09:43 PM
Last Post: deanhystad
  Use module docx to get text from a file with a table Pedroski55 8 5,788 Aug-30-2022, 10:52 PM
Last Post: Pedroski55
  Multiplication Table code alexsendlegames100 3 1,316 Jun-06-2022, 09:45 AM
Last Post: Gribouillis
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,672 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  How from sklearn.datasets import load_diabetes change to import to CSV file Anldra12 0 1,811 Dec-25-2021, 07:20 PM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020