You must have:
- Python 3.6+
- MySQL 8.0+: Page
- Have a MySQL user '
testuser
' with password 'mypassword
' - Knowledge in Python's virtualenv (venv). If you don't, follow the Installing virtualenv, Creating a virtual environment, Activating a virtual environment, and Leaving a virtual environment sections in the following link
- Knowledge with pip installations. If you don't, just follow the related sections in the link above.
There are more pointers about the repo on this file's Notes
section.
Since we're all using Python 3.6+ and I personally got MySQL8.0:
Connector/Python Version | MySQL Server Versions | Supported Python Versions |
---|---|---|
8.0 | 8.0, 5.7, 5.6, 5.5 | 3.6, 3.5, 3.4, 2.7 |
To install the MySQL Python connector, simply run:
pip install mysql-connector-python
If the installation fails, try running this one instead:
pip install mysql-connector-python==8.0.17
To verify installation, open up IDLE, and run the following code:
>>> import mysql.connector
>>> mysql.connector.connect(host='localhost',database='mysql',user='testuser',password='mypassword')
Related Files:
Follow the instructions in this link.
Related Files:
To query data in a MySQL database from Python, you need to do the following steps:
- Connect to the MySQL Database, you get a
MySQLConnection
object. - Instantiate a
MySQLCursor
object from the theMySQLConnection
object. - Use the cursor to execute a query by calling its
execute()
method. - Use
fetchone()
,fetchmany()
orfetchall()
method to fetch data from the result set. - Close the cursor as well as the database connection by calling the
close()
method of the corresponding object.
Related Files:
To insert new rows into a MySQL table, you follow these steps:
- Connect to the MySQL database server by creating a new
MySQLConnection
object. - Initiate a
MySQLCursor
object from theMySQLConnection
object. - Execute the
INSERT
statement to insert data into the table and commit the change. - Close the database connection.
Related Files:
To update data in a MySQL table in Python, you follow these steps:
- Connect to the database by creating a new
MySQLConnection
object. - Create a new
MySQLCursor
object from theMySQLConnection
object and call theexecute()
method of theMySQLCursor
object. To accept the changes, you call thecommit()
method of theMySQLConnection
object after calling theexecute()
method. Otherwise, no changes will be made to the database. - Close the cursor and database connection.
Related Files:
To delete data from a table from a Python program, you follow these steps:
- Connect to the database by creating a new
MySQLConnection
object. - Instantiate a new cursor object and call its
execute()
method. To commit the changes, you should always call thecommit()
method of theMySQLConnection
object after calling theexecute()
method. - Close the cursor and database connection by calling
close()
method of the corresponding objects.
Related Files:
To call a stored procedure in Python, you follow the steps below:
- Connect to the database by creating a new
MySQLConnection
object. - Instantiate a new
MySQLCursor
object from theMySQLConnection
object by calling thecursor()
method. - Call
callproc()
method of theMySQLCursor
object. You pass the stored procedure’s name as the first argument of thecallproc()
method. If the stored procedure requires parameters, you need to pass a list as the second argument to thecallproc()
method. In case the stored procedure returns a result set, you can invoke thestored_results()
method of theMySQLCursor
object to get a list iterator and iterate this result set by using thefetchall()
method. - Close the cursor and database connection as always.
Notice how in the two related python files: call_find_all_sp.py
and call_find_by_isbn.py
we don't actually write the stored procedures. We simply used the python files to call on those two already existing stored procedures. This means we have to actually define those stored procedures on the MySQL Workbench
or in the MySQL Shell
/ shell
application directly.
Related Files:
A walk through the code:
- First, we call the
read_file()
function to read data from a file and return it. - Second, we compose an
UPDATE
statement that updates photo column for an author specified byauthor_id
. The args variable is a tuple that contains file data andauthor_id
. We will pass this variable to theexecute()
method together with the query . - Third, inside the
try except
block, we connect to the database, instantiate a cursor, and execute the query withargs
. To make the change effective, we callcommit()
method of theMySQLConnection
object. - Fourth, we close the cursor and database connection in the
finally
block.
Related Files:
A walk through the code:
- First, compose a
SELECT
statement that retrieves the photo of a specific author. - Second, get the database configuration by calling the
read_db_config()
function. - Third, connect to the database, instantiate cursor, and execute the query. Once receiving the BLOB data, we use the
write_file()
function to write it into a file specified by thefilename
. - Finally, close the cursor and database connection.
- Unrelated to the tutorial, but still important: Make sure to
activate
the environment before running any of the files, anddeactivate
it when you're done with the files. - You should always use
commit()
after calling theexecute()
to make sure that the change on your table or database persists. - You should always use placeholders inside any query that you pass to the
execute()
method. This helps you avoid a SQL injection.
Original Tutorial was found here