Is there a module written purely in Python that will allow a script to communicate with a MySQL database? I\'ve already tried MySQLdb without success. It requires too much: GCC,
I had the problem that I wanted to write code which work for Python 2 and Python 3. I found pymysql to fit perfectly. I did not have to adjust code to switch from MySQLdb to pymysql
$ pip install PyMySQL
If you don't have admin rights:
$ pip install -u PyMySQL
The usage is the same as for MySQLdb:
import pymysql
import pymysql.cursors
connection = pymysql.connect(host=mysql['host'],
user=mysql['user'],
passwd=mysql['passwd'],
db=mysql['db'],
cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()
sql = ("SELECT `id`, `formula_in_latex` FROM `wm_formula` "
"WHERE `is_important` = 1 "
"AND id != 1 " # exclude trash class
"ORDER BY `id` ASC")
cursor.execute(sql)
formulas = cursor.fetchall()
import pymysql
connection = pymysql.connect(host=mysql['host'],
user=mysql['user'],
passwd=mysql['passwd'],
db=mysql['db'])
cursor = connection.cursor()
cursor.execute("INSERT INTO `toys` (`id`, `arrival`, `duration`) "
"VALUES ('1', '2014-12-01', '123');")
connection.commit()