2

after adding about 6k of lines into my mySQL database through Python, some letters are wrong ( polish letters), like Å‚ for ł and ż for ż and others. How can I repair that? My python code is:

 # -*- coding: utf-8 -*-
import MySQLdb
import string
import codecs
file = codecs.open("----", 'r', 'utf-8')
db = MySQLdb.connect(host="-----", port=3306, user="-----", passwd="------", db="----")
cursor = db.cursor()
for line in file:
        lines = string.replace(line, "'", '"')
        cursor.execute("INSERT INTO Logs (Text) VALUE ('%s')"% lines.encode("utf-8"))
db.close()
print("done")

and after running this code, it works normally , but in PhpMyAdmin, there is wrong letters. Coding in Database is UTF-8, file is in ANSI as UTF-8 (from notepad++). Any Help?

  • Are you sure PHPMyAdmin isn't just displaying it wrong? You might want to check out http://stackoverflow.com/questions/4777900/how-to-display-utf-8-characters-in-phpmyadmin – Nick Craig-Wood May 27 '12 at 16:19
  • It's displaying correctly, the answer down here is making it working. – Marcin Gordziejewski May 27 '12 at 16:24
  • 1
    use parameterized sql instead of Python string interpolation – jfs May 27 '12 at 16:30
  • Instead of `cursor.execute("INSERT INTO Logs (Text) VALUE ('%s')"% lines.encode("utf-8"))`, better do `cursor.execute("INSERT INTO Logs (Text) VALUE (%s)", lines.encode("utf-8"))`. – glglgl May 27 '12 at 21:58

1 Answers1

2

Specify the charset in MySQLdb.connect():

db = MySQLdb.connect(host="-----", port=3306, user="-----",
    passwd="------", db="----", charset='utf8')
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636