I'm trying to import 5 columns of data from a CSV to MySQL table. The first column of the MySQL is autoincrement, the first column of the CSV is blank. The following code produces an error.
import csv
import MySQLdb
database = MySQLdb.connect(host='10.200.10.125', user='spaine', passwd='spaine', db='scat')
cursor = database.cursor()
cursor.execute('show tables')
for row in cursor:
print row
cursor.execute('describe brewpubs')
print "\n info about table brewpubs"
for row in cursor:
print row
csv_file = 'E:\\Proving Grounds\\MsSQL Connection\\Brewery Data\\Breweries.csv'
csv_data = csv.reader(file(csv_file))
for row in csv_data:
print row
cursor.execute('''INSERT INTO brewpubs (PID, name, londd, latdd, desc) VALUES(%s, %s, %s, %s, %s)''', row)
database.commit() # Make sure data is committed to the database
database.close()
This code produces the following error:
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc) VALUES('', 'Hoppy Brewing', '-121.429684', '38.554962', 'http://www.hoppy.' at line 1")
Shadow :
desc is a reserved word in mysql, you have to enclose it by backticks (`) to use it as an identifier:\n\n... (PID, name, londd, latdd, `desc`)\n",
2016-04-12T22:09:18