I am trying to insert data in MySQL using Python. There is an error when inserted ,_, and ,:-) into database.
TypeError: not all arguments converted during string formatting
In the codes:
emoticons = 'C:/Users/user/Desktop/emoticons.txt'
csv_data = csv.reader(open(emoticons, 'rb'))
count = 0
for row in csv_data:
count = count + 1
c.execute("INSERT INTO Emoticons (Emotions) VALUES (%s)", row)
db.close()
Did I miss something in the codes? Any suggest?
reads file as CSV with comma as default separator and returns a list (the
rowvariable).awaits a tuple in
rowwith exactly one element (because there is one%sin the query string).Your file probably contains commas and these lines will be split into several elements which cannot be formatted into the query.
If you want to insert the whole lines from the file into DB, you don’t have to use
csv: