Home:ALL Converter>import CSV to MySQL error

import CSV to MySQL error

Ask Time:2016-04-13T06:05:29         Author:spaine

Json Formatter

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")

Author:spaine,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36584897/import-csv-to-mysql-error
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
yy