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 callingpymysql.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
You should probably use pymysql - Pure Python MySQL client instead.
It works with Python 3.x, and doesn't have any dependencies.
This pure Python MySQL client provides a DB-API to a MySQL database by talking directly to the server via the binary client/server protocol.
Example:
import pymysqlconn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')cur = conn.cursor()cur.execute("SELECT Host,User FROM user")for r in cur: print(r)cur.close()conn.close()
if you want to use MySQLdb first you have to install pymysql on your pc by typing in cmd of windows
pip install pymysql
then in python shell, type
import pymysql pymysql.install_as_MySQLdb() import MySQLdb db = MySQLdb.connect("localhost" , "root" , "password")
this will establish the connection.