我正在使用pymysql将csv文件上传到我的数据库中。
我使用以下代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pymysql
import codecs
import csv
import urllib2
import pymysql.cursors
# Get Data
csvfile = csv.reader(codecs.iterdecode(file('match_urls-2016-03-04', 'utf-8'))
# Connect to the database
connection = pymysql.connect(host='host.com', user='', password='', db='', cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
sql = "INSERT INTO Urlup(URL) VALUES(%s)"
next(csvfile, None)
for line in csvfile:
cursor.execute(sql, (line[0]))
finally:
connection.commit()
connection.close()
但我收到错误"连接"语法无效,不确定我应该使用什么,或者即使脚本可以正常工作
答案 0 :(得分:3)
您需要将查询参数作为元组传递,逗号有所不同:
cursor.execute(sql, (line[0], ))
# HERE^
而且,你错过了右括号:
csvfile = csv.reader(codecs.iterdecode(file('match_urls-2016-03-04', 'utf-8')))
# HERE^