[英]Using pymysql to upload csv to database
我正在使用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()
但我收到錯誤“連接”無效的語法,並不確定我應該使用什么,甚至腳本將工作
您需要將查詢參數作為元組傳遞,逗號會產生差異:
cursor.execute(sql, (line[0], ))
# HERE^
並且,您缺少右括號:
csvfile = csv.reader(codecs.iterdecode(file('match_urls-2016-03-04', 'utf-8')))
# HERE^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.