Some of the other significant capabilities that are worth mentioning are the fact that the application does support protocol compression, which offers data stream compression between the client and server and secure TCP/IP connections, using SSL certification.įurthermore, the application has a self-contained driver and it will not require any other Python modules apart from those provided in the standard library, which means that people will not need the MySQL client library. Using MySQL Connector/Python, one will be able to convert parameter values back and forth between Python and MySQL data types and this can be done either manually or automatically, in order to achieve increased performance. The utility is written in pure Python and its single dependencies include the Python Standard Library.Īvailable in both 32 and 64-bit installations, this application is currently compatible with versions of Python from 2.7 to 3.5, this way offering users a broad range of options when choosing the required installer. To achieve such connectivity, it will deploy an API that is compliant with the Python Database API Specification v2.0 (PEP 249). MySQL Connector/Python was developed in order to provide connectivity between Python-based programs and MySQL databases. The Cursor object enables you to run the execute() method, which in turn enables you to run raw SQL statements (in this case, a SELECT query on a table named employee).Those who rely heavily on Python for their various programming undertakings could, at some point, require to access other database environments, such as MySQL. When you have a Connection object associated with a database, you can create a Cursor object. Because all three MySQL modules use the portable SQL database API interface, they are able to use the code in the doQuery() function without any modifications. This example creates a series of Connection objects that opens the same database using different MySQL modules. M圜onnection = nnect( host=hostname, user=username, passwd=password, db=database ) M圜onnection = ( host=hostname, user=username, passwd=password, db=database ) # Simple routine to run a query on a database and print the results:Ĭur.execute( "SELECT fname, lname FROM employee" )įor firstname, lastname in cur.fetchall() : In your own code, replace username with the MySQL database username, password with the database user's password, and dbname with the database name: #!/usr/bin/python The sample code works with Python 2.7 and Python 3.x. The following sample Python code demonstrates how to do this, as well as just how easy it is to switch between the different SQL package implementations. To install the pymysql package, type the following command:Īfter you install a MySQL package in the virtual environment, you are ready to work with actual databases. To install the mysql-connector-python package, type the following command: To install the mysqlclient package, type the following command:.Type the command for the package you want to install: To update pip in the virtual environment, type the following command: If you log out of your SSH session (or deactivate the virtual environment by using the deactivate command), make sure you reactivate the virtual environment before following the steps below and running the sample code. All of the following commands in this procedure assume that you are working within the virtual environment. The command prompt now starts with (sqlenv) to indicate that you are working in a Python virtual environment. To create a virtual environment, type the following commands:.To set up the Python virtual environment and install a MySQL package, follow these steps: Setting up the Python virtual environment and installing a MySQL package This means that if you switch from one module to another, you can reuse almost all of your existing code (the code sample below demonstrates how to do this). It is written entirely in Python.Īll three of these packages use Python's portable SQL database API. PyMySQL: This package contains the pymysql module.mysql-connector-python: This package contains the nnector module.It is written in C, and is one of the most commonly used Python packages for MySQL. mysqlclient: This package contains the MySQLdb module.Setting up the Python virtual environment and installing a MySQL packageīefore you can access MySQL databases using Python, you must install one (or more) of the following packages in a virtual environment:.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |