How to Use MySQL ConnectorPython

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 Reading On: MySQL: Error 1264 Out of range value for a column

Leave a Reply

Your email address will not be published. Required fields are marked *