使用python将csv数据导入mysql数据库时出错

时间:2015-02-25 10:42:46

标签: python mysql csv

我编写以下代码将csv数据导入mysql数据库

#!/usr/bin/python
# coding: latin-1
#simple python csv parser that export Comma
#Separated Value data into a MySQL database.

import csv
import MySQLdb
# open the connection to the MySQL server.
# using MySQLdb
mydb = MySQLdb.connect(host='localhost',
user='root',
passwd='root',
db='teachers')
cursor = mydb.cursor()
# read the presidents.csv file using the python
# csv module http://docs.python.org/library/csv.html
csv_data = csv.reader(file('MOCK_DATA.csv'))
# execute the for clicle and insert the csv into the
# database.
for row in csv_data:

    cursor.execute('''INSERT INTO ts_info_course_info(Course_code ,Teacher_email_id ,Teacher_name ,Student_email_id ,Student_username ,Student_name ,Student_internal_roll_no , Quiz_no, Quiz_marks)'\'VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)''', row)
#close the connection to the database.
cursor.close()
print "Import to MySQL is over"

我收到以下错误:

Traceback (most recent call last):
  File "csv_to_db.py", line 22, in <module>
    cursor.execute('''INSERT INTO ts_info_course_info(Course_code ,Teacher_email_id ,Teacher_name ,Student_email_id ,Student_username ,Student_name ,Student_internal_roll_no , Quiz_no, Quiz_marks)'\'VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)''', row)
  File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.4b4-py2.7-linux-i686.egg/MySQLdb/cursors.py", line 184, in execute
    query = query % db.literal(args)
TypeError: not enough arguments for format strig

请帮我删除上述错误。

0 个答案:

没有答案