3

I am using pymysql to upload a csv file into my database.

I am using the following code:

    #!/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()

but I am getting the error "connection" invalid syntax and unsure what I am supposed to use or even if the script will work

enter image description here

1 Answer 1

3

You need to pass the query parameters as a tuple, the comma makes the difference:

cursor.execute(sql, (line[0], ))
#                       HERE^

And, you are missing the closing parenthesis:

csvfile = csv.reader(codecs.iterdecode(file('match_urls-2016-03-04', 'utf-8')))
#                                                                         HERE^ 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks - stil getting invalid syntax on connection = pymysql.con
@emmaperkins could you please post the complete error traceback you see? Thanks.
all i get is the posted in my main queston
@emmaperkins thanks for the additional information, updated the answer. Hope that helps.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.