The PyMySQL Package
Reference:
The PyMySQL
package provides a way for Python to interface with MySQL databases.
Run a PyMySQL
application "in development" using a database server on your local machine, and/or "in production" using a remote database server hosted by a provider like Heroku. If you run it in development, you should be able to connect via localhost, whereas if you run it in production, you should be able to connect using the production server's credentials. The professor recommends using Sequel Pro as a GUI interface to your MySQL databases, local or remote.
Installation
As a prerequisite: install MySQL on your local machine, if necessary. If you are on a Mac, use Homebrew: brew install mysql
and follow the post-installation instructions. Make sure you can connect to your local MySQL installation via a GUI or command-line interface. If attempting to connect from the command-line, try running mysql -uroot
or mysql -uroot -p
, depending on whether or not your "root" user has a password. Note the username and password you are using to connect.
After demonstrating your ability to connect to a local MySQL installation, install PyMySQL
, if necessary:
Usage
Place the following contents inside a new Python script:
Finally, run the script to see the results of your SQL query output into the terminal. Oh yea!
Now that you know how to use Python to execute a SQL query, practice using Python to manage databases and tables, then populate them and query them.
Last updated
Was this helpful?