Sep. 12, 2015

How to Use MySQL Connector/Python

MySQL Connector/Python is a driver released by Oracle themselves to make it easier to connect to a MySQL database with Python. MySQL Connecter/Python supports all versions of Python 2.0 and later, also including versions 3.3 and later.

To install MySQL Connector/Python simply use pip to install the package.


pip install mysql-connect-python --allow-external mysql-connector-python

Connecting to database and committing to it is very simple.


import mysql.connector

connection = mysql.connector.connect(user="username", 
                                     password="password", 
                                     host="127.0.0.1", 
                                     database="database_name")

cur = connection.cursor()

cur.execute("INSERT INTO people (name, age) VALUES ('Bob', 25);")
connection.commit()

cur.close()
connection.close()

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.

 



Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.