1

I have a MySQL database defined with collation UTF8. A table 'clientes' and all its columns are specified as UTF8.

DROP TABLE IF EXISTS `clientes`;
CREATE TABLE IF NOT EXISTS `clientes` (
  `ID` varchar(35) NOT NULL DEFAULT '',
  `nombre` varchar(255) NOT NULL DEFAULT '',
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `clientes` (`ID`, `nombre`) VALUES
('111', 'María Español Marqués'),
('222', 'Paulo Conceiçao'));

Queries with MySQL clients (in terminal, phpmyadmin...) displays characters fine:

mysql> select * from clientes;
+-----+--------------------------+
| ID  | nombre                   |
+-----+--------------------------+
| 111 | María Español Marqués    |
| 222 | Paulo Conceiçao          |
+-----+--------------------------+
2 rows in set (0.00 sec)

Now, I create the class with SQLAlchemy:

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref

engine = create_engine('mysql://root:toor@localhost')
engine.execute("USE nTal")
Base = declarative_base()

class Cliente(Base):

    __tablename__ = "clientes"

    ID = Column(String(35), primary_key=True)
    nombre = Column(String(255))

    def __init__(self, nombre):
        """"""
        self.nombre = nombre

engine.dispose()

When I run a query with SQLAlchemy, I get odd characters with accents, spanish and portuguese simbols are not visible.

res = session.query(Cliente).all()
for cliente in res:
    print cliente.ID + ", " + cliente.nombre

111, Mar�a Espa�ol Marqu�s
222, Paulo Concei�ao

Of course, I have search a lot about enconding and decoding, but the most I find is about errors, not about incorrect displaying.

Thanks in advance.

6
  • Does your font even contain spanish haracters? Commented Feb 10, 2014 at 20:12
  • Yes, of course: I'm spanish... Commented Feb 10, 2014 at 20:17
  • Tell more about your OS. How is locale configured? Commented Feb 10, 2014 at 20:17
  • Ubuntu 13.10, locale: LANG=es_ES.UTF-8 LANGUAGE=es_ES LC_CTYPE="es_ES.UTF-8", and so on... The odd characters are obtained in PyCharm IDE and also in terminal. Commented Feb 10, 2014 at 20:18
  • Can you figure out character codes of these characters? Are they all replaced by � or this is only a replacement character showed instead of character missing in font? Commented Feb 10, 2014 at 20:32

1 Answer 1

3

Try adding "?charset=utf8" to the connection string:

engine = create_engine('mysql://root:******@localhost?charset=utf8')
Sign up to request clarification or add additional context in comments.

1 Comment

Nice. If you would use mysql+mysqlconnector (and Install MySQL Connector/Pyton), I think it should work without the extra option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.