Tornado provides a simple MySQL wrapper for performing database operations. Class tornado.databse.Connection acts as a wrapper over MySQLdb DB-API connection.
Consider you have a MySQL installed on your system and you create a DB ‘mydb’ with table ‘post’ and records as below:
mysql> use mydb Database changed mysql> create table post (Id int, Title char(50), Author char(50)); Query OK, 0 rows affected (0.03 sec) mysql> insert into post values (1, 'Tornado Database', 'TechnoBeans'); Query OK, 1 row affected (0.01 sec) mysql> insert into post values (2, 'Tornado Authentication', 'TechnoBeans'); Query OK, 1 row affected (0.00 sec) mysql> insert into post values (3, 'Tornado Locale', 'TechnoBeans'); Query OK, 1 row affected (0.00 sec) mysql> select * from post; +------+------------------------+-------------+ | Id | Title | Author | +------+------------------------+-------------+ | 1 | Tornado Database | TechnoBeans | | 2 | Tornado Authentication | TechnoBeans | | 3 | Tornado Locale | TechnoBeans | +------+------------------------+-------------+ 3 rows in set (0.02 sec)
Tornado helps you to connect, retrieve and display records from ‘mydb’ MySQL DB as shown in the example below
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.ioloop | |
import tornado.web | |
import tornado.database | |
class Main(tornado.web.RequestHandler): | |
def get(self): | |
self.write("Main") | |
class DBHandler(tornado.web.RequestHandler): | |
def get(self): | |
db = tornado.database.Connection( | |
host="localhost", database="mydb", | |
user="root", password="root") | |
rows = db.query("select Id,Title from post") | |
db.close() | |
top = "<html><body><b>Posts</b><br /><br />" | |
table = "<table border=\"1\"><col width=\"50\" /><col width=\"200\" />" | |
for row in rows: | |
table += "<tr><td>" + str(row["Id"]) + "</td><td>" + str(row["Title"]) + "</td></tr>" | |
bottom = "</body></html>" | |
self.write(top+table+bottom) | |
application = tornado.web.Application([ | |
(r"/", Main), | |
(r"/posts", DBHandler), | |
]) | |
if __name__ == "__main__": | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
Output
In this example:
1. tornado.database.Connection connects to MySQL DB instance with appropriate server, database name, username and passwd
2. Object of class tornado.database.Connection ‘db’ is then used to query the records of table ‘post’
3. DB connection is then closed with db.close()
4. Records are then rendered by the webserver on accessing http://localhost/posts URL
Note:
While trying out this example, you may bump into this error
ubuntu@ubuntu:~/tornado-2.2$ python tornadodatabase.py Traceback (most recent call last): File "tornadodatabase.py", line 4, in import tornado.database File "/home/ubuntu/tornado-2.2/tornado/database.py", line 20, in import MySQLdb.constants ImportError: No module named MySQLdb.constants
This is because python-mysqldb is not installed on your system. On ubuntu systems, you can get it with
sudo apt-get install python-mysqldb
Great!
is there anything similar for SQLite?
Ye, here it is http://technobeans.com/2012/08/25/tornado-sqlite/