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
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
Extract Elements of List at Odd Positions
How to Decorate All Functions of a Class Without Typing It Over and Over for Each Method
Parsing HTML in Python - Lxml or Beautifulsoup? Which of These Is Better for What Kinds of Purposes
How to Update JSON File with Python
How to Create a Read-Only Class Property in Python
Why Does Python Pep-8 Strongly Recommend Spaces Over Tabs for Indentation
Why Is Looping Over Range() in Python Faster Than Using a While Loop
How to Check If a File Is a Valid Image File
Can Iterators Be Reset in Python
How to Point Easy_Install to Vcvarsall.Bat
Where Is Python's "Best Ascii for This Unicode" Database
How to Fit a Sine Curve to My Data with Pylab and Numpy
Thread Safety in Python's Dictionary
Display Special Characters When Using Print Statement
How to Get the Difference Between Two Dictionaries in Python
What Is the Most Efficient Way to Get First and Last Line of a Text File
How to Get System Timezone Setting and Pass It to Pytz.Timezone