Seth M. answered 02/07/20
Learn Python with Friendly, Personalized, Expert Tutoring!
MySQL suggests using "MySQL Connector" to link Python and a MySQL database. For complete details, see https://dev.mysql.com/doc/connector-python/en/.
Briefly, if you already have Python installed, you will usually need to install the connector. The installer is available at the MySQL website, presently at this address: https://dev.mysql.com/downloads/connector/python/. The precise installation process depend on your operating system.
Once installed, using it is similar to using any other Python library (with an import command at the top of the code). For example:
import mysql.connector
Once imported, executing SQL commands is very similar to the procedure followed in most common languages:
- Establish a connection to the database
- Construct an SQL string
- Execute the SQL command (Python/MySQL-Connector uses a "cursor()" method to do this)
- Collect and evaluate any results
- Close the cursor and DB connection