Integrating MySQL with Python in Windows

How can I connect to MySQL in Python 3 on Windows?

There are currently a few options for using Python 3 with mysql:

https://pypi.python.org/pypi/mysql-connector-python

  • Officially supported by Oracle
  • Pure python
  • A little slow
  • Not compatible with MySQLdb

https://pypi.python.org/pypi/pymysql

  • Pure python
  • Faster than mysql-connector
  • Almost completely compatible with MySQLdb, after calling pymysql.install_as_MySQLdb()

https://pypi.python.org/pypi/cymysql

  • fork of pymysql with optional C speedups

https://pypi.python.org/pypi/mysqlclient

  • Django's recommended library.
  • Friendly fork of the original MySQLdb, hopes to merge back some day
  • The fastest implementation, as it is C based.
  • The most compatible with MySQLdb, as it is a fork
  • Debian and Ubuntu use it to provide both python-mysqldb andpython3-mysqldb packages.

benchmarks here: https://github.com/methane/mysql-driver-benchmarks

Connection to remote SQL Server via Windows Credentials

Solved my own question.

import pyodbc

conn = pyodbc.connect('Driver={SQL Server};'
'Server=GDC-CXL-DB-PROD.zug.novatek.int;'
'Database=CXLPROD;'
'Trusted_Connection=yes;')
SQL = {string}

data = pd.read_sql(SQL,conn)

Windows Authentication with MySQLdb in Python

Based on Jaco's answer but pyodbc library requires slightly different string, or it will produce unrecognized attribute error:

Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;Uid=auth_window;

What is the process for using MySQL from Python in Windows?

If you just want to use the DBAPI, then here's a simple snippet explaining how to issue a SELECT query.

import MySQLdb
db = MySQLdb.connect(host="host", user="username", passwd="your-pass", db="the-db-name")

To perform a query, you first need a cursor, and then you can execute queries on it:

cursor = db.cursor()
max_age = 42
cursor.execute("""SELECT name FROM employees WHERE age < %s""", (max_age,))
print cursor.fetchone()

However, you most likely want to use an ORM, I recommend SQLAlchemy. It essentially trivializes database interaction by providing a super-powerful abstraction layer.

Mysql Connector 8.0 and Python 3.8

I am not sure why I was using the MySQL installer to install libraries for Python. In hind-sight, that doesn't make sense. Use pip to install the drivers like

$ pip install mysql-connector-python
# or
$ python pip install mysql-connector
# or for >= Python 3.9
$ pip install mysqlclient

It is not entirely clear what the difference is, but, the first of the three solved the problem.



Related Topics



Leave a reply



Submit