我是python.py的新手,我想用mysql.connector和python連接mysql。我想打開一個csv,然後把它上傳到mysql數據庫。我用的是python版本:3.6.0用python連接到mysql並上傳csv
我已經試過這一個:
import csv
import mysql.connector
cnx = mysql.connector.connect(user='', password='',
host='127.0.0.1',
database='')
csv_data = csv.reader(file('12_13.csv'))
for row in csv_data:
cursor.execute('INSERT INTO testcsv(names, \
classes, mark)' \
'VALUES("%s", "%s", "%s")',
row)
#close the connection to the database.
mydb.commit()
cursor.close()
print("Done")
我收到此錯誤:
Traceback (most recent call last): File ".\uploadtomysql.py", line 7, in csv_data = csv.reader(file('12_13.csv')) NameError: name 'file' is not defined
謝謝!