How to Connect to MySQL in Python 3 on 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

Python 3.4.0 with MySQL database

MySQLdb does not support Python 3 but it is not the only MySQL driver for Python.

mysqlclient is essentially just a fork of MySQLdb with Python 3 support merged in (and a few other improvements).

PyMySQL is a pure python MySQL driver, which means it is slower, but it does not require a compiled C component or MySQL libraries and header files to be installed on client machines. It has Python 3 support.

Another option is simply to use another database system like PostgreSQL.

How do I install and use MySQLdb for Python 3 on Windows 10?

You could use pymysql. "The goal of PyMySQL is to be a drop-in replacement for MySQLdb". Check the docs here. Install the following libraries

pip install mysqlclient pymysql

Once these libraries are installed, just add the lines in the manage.py file in your project and use the database settings for mysql.

import pymysql

pymysql.install_as_MySQLdb()

Now any files that import MySQLdb will work.

How to install MySQL Connector for Python on Windows when according to the installation manager Python is not installed?

I could finally do it! In case someone else goes through the same nightmare as I had, here is the solution: instead of using command line I opened up Windows Power Shell and typed

pip install mysql-python

And it succeeded in downloading and Python script doesn't throw error anymore.

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)


Related Topics



Leave a reply



Submit