> Stop Using MySQLDb if you want to avoid installing mysql headers just to access mysql from python.
Use [pymysql][1]. It does all of what MySQLDb does, but it was implemented purely in Python with ***NO External Dependencies***. This makes the installation process on all operating systems consistent and easy. `pymysql` is a drop in replacement for MySQLDb and IMHO there is no reason to ever use MySQLDb for anything... EVER! - *`PTSD from installing MySQLDb on Mac OSX and *Nix systems`*, but that's just me.
**Installation**
`pip install pymysql`
> That's it... you are ready to play.
**Example usage from pymysql Github repo**
import pymysql.cursors
import pymysql
# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ('
[email protected]', 'very-secret'))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
cursor.execute(sql, ('
[email protected]',))
result = cursor.fetchone()
print(result)
finally:
connection.close()
> **ALSO - Replace MySQLdb in existing code quickly and transparently**
If you have existing code that uses MySQLdb, you can easily replace it with pymysql using this simple process:
# import MySQLdb << Remove this line and replace with:
import pymysql
pymysql.install_as_MySQLdb()
All subsequent references to MySQLdb will use pymysql transparently.
[1]:
[To see links please register here]