Skip to content

Instantly share code, notes, and snippets.

@skyowen
Last active November 21, 2020 03:37
Show Gist options
  • Save skyowen/8493812 to your computer and use it in GitHub Desktop.
Save skyowen/8493812 to your computer and use it in GitHub Desktop.
import csv
import MySQLdb
# open the connection to the MySQL server.
# using MySQLdb
mydb = MySQLdb.connect(host='igor.gold.ac.uk', user='co304so', passwd='*********', db='co304so_LondonCrime')
cursor = mydb.cursor()
csv_data = csv.reader(file('1.csv'))
# execute and insert the csv into the database.
for row in csv_data:
cursor.execute('INSERT INTO AprilNov2013(Date,Westminster,HACKNEY,Tower_Hamlets)''VALUES(%s, %s, %s, %s)',row)
print row
#close the connection to the database.
mydb.commit()
cursor.close()
print "CSV has been imported into the database"
@Shenthil
Copy link

Shenthil commented Jun 6, 2018

Traceback (most recent call last):
File "C:/Python27/New folder/csvimport.py", line 11, in
'VALUES(%%s%, %%s%, %%s%, %%%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%, %%s%)', row)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 159, in execute
query = query % db.literal(args)
ValueError: unsupported format character ',' (0x2c) at index 288

I do not know why this error occurs.

@aftab70
Copy link

aftab70 commented May 3, 2019

useful source code.

@Nagendra303
Copy link

Nagendra303 commented Nov 22, 2019

getting the following error while executing the given code:
mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment