martinqt
martinqt

Reputation: 719

Python 3 and mysql through SQLAlchemy

Currently:

The problem is that MySQLdb or Oursql is required and I didn't managed to get any of them working.

Found this but didn't manage to get it working neither.

Edit: If you are aware of an other orm that works with Python3, I'm interested.

Upvotes: 12

Views: 37373

Answers (5)

Wesley Cheek
Wesley Cheek

Reputation: 1696

Here is a modern solution for the pymysql driver using the do connect event

DBHostname = DBCLUSTER_HOSTNAME
DBPort = DBCLUSTER_PORT
DBUsername = DBCLUSTER_USER
DBName = DBCLUSTER_NAME

engine = sqlalchemy.create_engine(
    "mysql+pymysql:///"
)  # connection params will be set by the event callback

@sqlalchemy.event.listens_for(engine, "do_connect")
def provide_token(dialect, conn_rec, cargs, cparams):

    # set up db connection parameters, alternatively we can get these from boto3 describe_db_instances
    cparams["host"] = DBHostname
    cparams["port"] = int(DBPort)
    cparams["user"] = DBUsername
    cparams["password"] = password
    cparams["database"] = DBName

Upvotes: 0

Brad Campbell
Brad Campbell

Reputation: 3071

I was successful in getting Oracle's MySQL connector for python working with SQLAlchemy on Python 3.3. Your connection string needs to start with "mysql+mysqlconnector://...". After I changed my connection string everything (well, simple things) started working.

The MySQL connector docs can be found here: https://dev.mysql.com/doc/connector-python/en/

The package is up on PyPi: https://pypi.org/project/mysql-connector-python/

Here are the SQLAlchemy docs about using the Python connector: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqlconnector

Upvotes: 32

seaders
seaders

Reputation: 4096

I tried Oracle's connection, as suggested by @Brad Campbell, but unfortunately it was extremely slow, much slower than the "real" MySQL-Python connection I had been using with SQLAlchemy on Python 2.

After checking SQLAlchemy themselves,

http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqldb

To use MySQL-Python on Python 3, they recommend a fork of it, mysqlclient,

https://github.com/PyMySQL/mysqlclient-python

It is available via pip with pip install mysqlclient, but there are almost certainly other steps you'll need to do to set it up initially. After that though, I was seeing the performance go back to what I was used to, which was about 5x faster than with Oracle's connector.

Upvotes: 2

Fletch F Fletch
Fletch F Fletch

Reputation: 451

For others who arrive here, this should do it:

  • pip install mysql-connector==2.1.4 # version avoids Protobuf error
  • URI = 'mysql+mysqlconnector://$USER:$PASS@$HOST/$DB'

Upvotes: 13

mrgrieves
mrgrieves

Reputation: 579

I've gotten oursql + SQLAlchemy 0.8.1 + Python 3.3 to work. Building off of LukeCarrier's port, I modified oursql.c to use the correct import levels, and it worked! Try this, and be sure to follow the readme:

https://github.com/clintron/py3k-oursql

You may also need to have the latest version of Cython.

Upvotes: 1

Related Questions