MySQL :: MySQL Connector/Python Developer Guide :: 5.1 Connecting to MySQL Using Connector/Python
Technology

MySQL :: MySQL Connector/Python Developer Guide :: 5.1 Connecting to MySQL Using Connector/Python

MySQL :: MySQL Connector/Python Developer Guide :: 5.1 Connecting to MySQL Using Connector/Python

How to connect mysql to python

Video How to connect mysql to python

The connect() constructor creates a connection to the mysql server and returns a mysqlconnection object.

The following example shows how to connect to the mysql server:

Section 7.1, “python/plugin connection arguments” describes the allowable connection arguments.

It is also possible to create connection objects using the connection.mysqlconnection() class:

Both ways (either using the connect() constructor or the class directly) are valid and functionally the same, but connect() 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:

defining connection arguments in a dictionary and using the ** operator is another option:

using the python or c python connector/python extension

connector/python offers two implementations: a pure python interface and a c extension that uses the mysql c client library (see Chapter 8, the connector/python c extension). this can be set at runtime using the use_pure connection argument. defaults to false as of mysql 8, which means the c extension is used. if the c extension is not available on the system, use_pure defaults to true. setting use_pure=false causes the connection to use the c extension if your connector/python installation includes it, while setting use_pure=true to false means the python implementation is used if available.

The following example shows how to set use_pure to false.

It is also possible to use the c extension directly by importing the _mysql_connector module instead of the mysql.connector module. for more information, see section 8.2, “the _mysql_connector c plugin”.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button