0

I was trying to connect the mysql with python.

import mysql.connector
db = mysql.connector.connect()

Uptill this point everything was fine. But after this when I started specifying the attributes of the connect command, using the code bellow

import mysql.connector
db = mysql.connector.connect(
 host = "localhost", #specifies the host computer(in this case this computer only)
 user = "root", #specifies the user
 passwd = "<my password was here>"
 )

this error came up on running the code,

Traceback (most recent call last):
 File "C:/Users/Co-valent Cyclist/Desktop/Python + MySQL/test 1.py", line 6, in <module>
 passwd = "co_valent_bonds"
 File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\__init__.py", line 177, in connect
 return MySQLConnection(*args, **kwargs)
 File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 104, in __init__
 self.connect(**kwargs)
 File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 785, in connect
 self._post_connection()
 File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 757, in _post_connection
 self.set_charset_collation(self._charset_id)
 File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 716, in set_charset_collation
 charset_name, collation_name))
 File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 970, in _execute_query
 self.cmd_query(query)
 File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 590, in cmd_query
 result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
 File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 478, in _handle_result
 raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1115 (42000): Unknown character set: 'utf8mb4'

how do I rectify this? Please help me out. Thanks!!

asked Dec 31, 2019 at 12:27
2
  • passwd = r"co_valent_bonds" Commented Dec 31, 2019 at 12:35
  • Hi, I tried this out but it is still not working and the same error is showing up. Please help me out. Commented Feb 16, 2020 at 9:42

1 Answer 1

1

Use charset="utf8" while creating the connection object .

If your characters actually use the superset utf8mb4 then this may cause problems, but if not, then this should work!

import mysql.connector
db = mysql.connector.connect(
 host = "localhost", #specifies the host computer(in this case this computer only)
 user = "root", #specifies the user
 passwd = "<my password was here>",
 charset = "utf8"
 )
answered Jul 28, 2021 at 13:21
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.