Issue with MySQLDB 'utf-8' codec can't decode byte 0x92

Viewed 1570

I have an issue running a query using MySQLdb. I have the following error message

': 'utf-8' codec can't decode byte 0x92 in position 2: invalid start byte

I have been able to run this query in the past without issue of encoding and therefore I don't get where my issue comes from. The only change I have done is downloading Postgres on my laptop and removing (accidentally) a db.sqlite3 file. But I don't understand why this will impact the encoding of my query especially since I don't have any special characters. Running a more simple query works fine and running this query from my colleague laptop also works.

The query is as following:

SLEEPERS_QUERY = """
select * from candidates"""
import MySQLdb as db


with SSHTunnelForwarder(
    ssh_address_or_host = host,
    ssh_port = 22,
    ssh_username = ssh_username,
    ssh_pkey = ssh_private_key,
    remote_bind_address = (rds, 3306), # 3306 = mysql port
) as server:
    server.start()
    print('Connected to the SSH server')

    while True:
        try:
            conn = db.connect(
                host = localhost,
                port = server.local_bind_port,
                user = user,
                passwd = password,
                db = database
            )
            print('Connected to the database server')
            break

        except: pass
    df = pd.read_sql_query(query,conn)

return df
1 Answers

I found a solution: my default encoding changed to from latin1 to utf8.

I had to add charset = 'latin1'

conn = db.connect(
            host = localhost,
            port = server.local_bind_port,
            user = user,
            passwd = password,
            db = database,
            charset = 'latin1'
            )
Related