Reputation: 666
Trying to import a list into MySQL through Python with MySQLdb...code below.
I get a ValueError when I run it -
query = query % tuple([db.literal(item) for item in args])
ValueError: unsupported format character ',' (0x2c) at index 51
Code_
import MySQLdb
import ystockquote
import re
mydb = MySQLdb.connect(host='localhost',
user= '****',
passwd='****',
db='****')
cur = mydb.cursor()
name = raw_input('please input a symbol_')
data = ystockquote.get_all(name)
stock = data.values()
clean = (", ".join(stock))
#var_string = ', '.join('?' * len(clean))
query_string = 'INSERT IGNORE INTO YAHOO VALUES (\"%s\");' % clean
cur.execute(query_string, clean)
print clean
When I print query_string the statement looks good and the 'clean' values appear to be in good shape - any help would be kindly appreciated.
print clean
1.52, -59.49%, 0.93, 0.17, 800, "1.51 - 1.59", 1.54, 1.59, N/A, -0.01, +5.25%, 0.16, N/A, 0.08, -2.32, N/A, 6.90M, -1.24, N/A, N/A, 1.50, 100, 326754, N/A, N/A, N/A, N/A, 1.51, "NYQ", N/A, "0.93 - 3.90", N/A, "3:48pm - <b>1.58</b>", "Ciber, "+3.95%", N/A, 3700, N/A, N/A, -34.46%, N/A, 2.84, N/A, N/A, N/A, 0.65, N/A, 127.47M, N/A, +69.89%, 2.63, N/A, N/A, 693036, 1.58, -0.65, 73289000, N/A, N/A, -0.83, 0.53, 80680000, N/A, N/A, N/A, Inc. Common Stock", 3.90, "6/14/2016", -0.16, N/A, N/A, 760.00M, +0.06, N/A, N/A, N/A, 2.41, "3:48pm", N/A, "+0.06 - +3.95%", 326754
print query_string
INSERT IGNORE INTO YAHOO VALUES ("1.52, -59.49%, 0.93, 0.17, 800, "1.51 - 1.59", 1.54, 1.59, N/A, -0.01, +5.25%, 0.16, N/A, 0.08, -2.32, N/A, 6.90M, -1.24, N/A, N/A, 1.50, 100, 335969, N/A, N/A, N/A, N/A, 1.51, "NYQ", N/A, "0.93 - 3.90", N/A, "3:51pm - <b>1.58</b>", "Ciber, "+3.95%", N/A, 3700, N/A, N/A, -34.46%, N/A, 2.84, N/A, N/A, N/A, 0.65, N/A, 127.47M, N/A, +69.89%, 2.63, N/A, N/A, 693036, 1.58, -0.65, 73289000, N/A, N/A, -0.83, 0.53, 80680000, N/A, N/A, N/A, Inc. Common Stock", 3.90, "6/14/2016", -0.16, N/A, N/A, 760.00M, +0.06, N/A, N/A, N/A, 2.41, "3:51pm", N/A, "+0.06 - +3.95%", 335969");
Upvotes: 1
Views: 110
Reputation: 12772
.execute
expect 2 arguments when using with wildcards or string replacement in the following manner:
%s
%s
you don't need to manually "clean" your values. I also included the column names in the query to make it clear:
data = ystockquote.get_all(name)
stock = data.values()
columns = data.keys()
columns[columns.index('change')] = '_change'
var_string = '%s' + ',%s' * (len(stock)-1))
query_string = '''
INSERT IGNORE INTO YAHOO
('''+','.join('`%s`' % (c,) for c in columns)+''')
VALUES (%s);''' % var_string
cur.execute(query_string, tuple(stock))
Upvotes: 1