Re: Cannot display MySQL latin2_croation_ci characters in Python script
Hi Paul,
Just to confirm, you are using 'latin2' or ISO 8859-2 character encoding. The collation is not really important here.
You did not say where you are displaying your data, but I'm guessing you are using some shell, using Gnome Terminal or Apple's? If you are using a console/terminal, make sure it is using 'latin2' encoding.
The bellow script shows how to do it. Make sure to save it as latin2. If you execute it, it should print correctly. Note how the table is created. And, again, make sure that the terminal where you print the data is using latin2 (ISO 8859-2).
-Geert
#!/usr/bin/env python
# -*- coding: latin2 -*-
import mysql.connector
DBCONFIG = {
'user': 'root',
'database': 'test',
'use_unicode': False,
'charset': 'latin2',
}
cnx = mysql.connector.connect(**DBCONFIG)
cur = cnx.cursor()
cur.execute("DROP TABLE IF EXISTS croatian")
cur.execute(
"CREATE TABLE croatian (c1 VARCHAR(100)) "
"CHARACTER SET 'latin2' COLLATE 'latin2_croatian_ci'")
data = [
("Karadžić",),
("Stanišić & Simatović",),
("Boškoski & Tarčulovski",),
("Đorđević",),
("Ražnatović, Željko",),
]
cur.executemany("INSERT INTO croatian (c1) VALUES (%s)", data)
cur.execute("SELECT c1 FROM croatian")
for row in cur:
print(row[0])
cnx.close()
Geert Vanderkelen
Software Developer at Oracle