Carpet
Carpet

Reputation: 519

Creating a Connection Between MySQL and Tornado

I am trying to get a Python and Tornado environment up and running.

As of now I am able to execute Python scripts and now I am trying to be able to make use of databases as well.

By my understanding Tornado has a MySQL wrapper, and I as of now I have XAMPP installed and I would like to continue using PhpMyAdmin as the GUI for MySQL.

The question I am having is how can I create a connection between MySQL and Tornado?

So that when you use a connection command Tornado will connect to the right MySQL installation and databases, which I of course created with PhpMyAdmin?

Upvotes: 3

Views: 3048

Answers (1)

hymloth
hymloth

Reputation: 7045

From Tornado's documentation:

db = database.Connection("localhost", "mydatabase")

Once you instantiate a connection ( named db in this example ), you can reuse it during your server's lifetime..

If you need to change it dynamically while your tornado server is running, then have tornado "listen" to a specific url_pattern, handled by the appropriate web.RequestHandler, which receives as (GET or POST) arguments your MYSQL connection parameters (host, database, user etc..) and creates a new database connection.

Edit

In newer versions of tornado (>=3.0) the tornado.database module has been removed. It is now available as a separate package, torndb.

Upvotes: 3

Related Questions