Skip to content Skip to sidebar Skip to footer

Python And Mysql: Is There An Alternative To Mysqldb?

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, zli

Solution 1:

You've looked at these?

http://wiki.python.org/moin/MySQL

Solution 2:

Lately there is also oursql (docs), which has various advantages over MySQLdb and other existing drivers -- listed at the top of the documentation.

Solution 3:

Oracle implemented a pure python mysql connector, aptly named mysql-connector-python. It can be pip-installed and receives plenty of updates.

Note that it's GPL licensed, which may or may not be a problem for your prjoect. If you can't live with that, you can also pick up PyMySQL instead which is one of the few connectors that works well and is distributed under MIT.

Solution 4:

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

Installation

$ pip install PyMySQL

If you don't have admin rights:

$ pip install -u PyMySQL

Usage

The usage is the same as for MySQLdb:

SELECT

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()

INSERT

importpymysqlconnection= 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()

Solution 5:

You can find pre-built binary packages for MySQLdb and its dependencies for most operating systems.

http://sourceforge.net/project/showfiles.php?group_id=22307&package_id=15775

What platform are you running on?

Post a Comment for "Python And Mysql: Is There An Alternative To Mysqldb?"