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
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 235 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Need Help with connecting to mysql from txt file kingceasarr 4 683 Mar-24-2021, 05:42 AM
Last Post: buran
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 1,818 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  Integrate/import a gui code to my main.py penahuse 6 589 Feb-03-2021, 08:49 PM
Last Post: deanhystad
  Python - Import file sequence into Media Pool jensenni 1 419 Feb-02-2021, 05:11 PM
Last Post: buran
  write mariadb table rows query to each file? shams 1 570 Feb-02-2021, 04:10 PM
Last Post: buran
  Creating table in MySQL db with decimal number issue dangermaus33 7 767 Nov-20-2020, 10:40 PM
Last Post: dangermaus33
Smile Import error with local file colt 1 533 Nov-08-2020, 07:56 AM
Last Post: Gribouillis
  I try to import data from Excel table to Word Template. NewbiePyPy 0 682 Oct-21-2020, 12:25 PM
Last Post: NewbiePyPy
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 954 Sep-04-2020, 01:39 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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