Connecting to MySQL Using Connector/Python

Example 1

Example 2
It is also possible to create connection objects using the connection.MySQLConnection() class. Both methods, using the connect() constructor, or the class directly, are valid and functionally equal, but using connector() is preferred and is used in most examples in this manual.

To handle connection errors, use the try statement and catch all errors using the errors.Error exception:

Example 3
If you have lots of connection arguments, it’s best to keep them in a dictionary and use the ** operator: