Milano
Milano

Reputation: 18705

Connection to MySql server fails - Python

I'm trying to connect to mySql database using Python. The database is situated on free webhosting server webzdarma.cz.

I use mysql.connector.connect and all of my arguments are correct in my opinion. The information about connecting to the database are:

This information is for PHP but I suppose it should work with Python:
Server: mysql.webzdarma.cz
Username: flat
Database: flat
Password: xxxx  

This is my code:

# -*- coding: utf-8 -*-
    import mysql.connector

    cnx = mysql.connector.connect(user='flat', password='xxxx',
                                  host='mysql.webzdarma.cz',
                                  database='flat')

When try to run this code the error occure:

    Traceback (most recent call last):
  File "C:/Users/Python/PycharmProjects/Flat/Flat.py", line 6, in <module>
    database='flat')
  File "C:\Python27\lib\site-packages\mysql\connector\__init__.py", line 159, in connect
    return MySQLConnection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 129, in __init__
    self.connect(**kwargs)
  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 454, in connect
    self._open_connection()
  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 417, in _open_connection
    self._socket.open_connection()
  File "C:\Python27\lib\site-packages\mysql\connector\network.py", line 470, in open_connection
    errno=2003, values=(self.get_address(), _strioerror(err)))
  File "C:\Python27\lib\site-packages\mysql\connector\errors.py", line 181, in __init__
    self.msg = self.msg % values
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf8 in position 15: ordinal not in range(128)

This error is raised because program recieved message in czech language about failed connection. Do anybody know what am I doing wrong?

Upvotes: 0

Views: 2447

Answers (3)

StefanE
StefanE

Reputation: 7630

I ran into the very same problem on my local dev machine and it turns out that my MySQL service was not started and I got the same error message. Started the instance and no problem any longer!

I will see if I can report this as an bug with the MySQL Connector.

Upvotes: 0

kid
kid

Reputation: 153

import MySQLdb 
db = MySQLdb.connect(host="mysql.webzdarma.cz", 
                 user="flat", 
                 passwd="xxxx", 
                 db="flat")
cur = db.cursor()
cur.execute("SELECT * FROM YOUR_TABLE_NAME")
res = cur.fetchall()

Try this it will work for sure.

for more details follow question asked How do I connect to a MySQL Database in Python?

Upvotes: 1

jsbueno
jsbueno

Reputation: 110146

If it is as you say, then you are doing nothing wrong - the MySQL provider is.

The way to avoid this error would be to set the default Python encoding to the one sent back by the remote side, so that it "understands" the error message when it gets there.

To do that, try to do: import sys sys.setdefaultencoding("")

I don't know which codec it might be - so if you don't have an easy way to know that, use "latin1" as the codec - it will give you a mojibake error message, but since all 256 byte codes are mapped to unicode, you won't get a UnicodeDecodeError.

Upvotes: 0

Related Questions