1, TORNDB Database Introduction
Prior to the Tornado3.0 version, the Tornado.database module was used to operate the MySQL database, and since the 3.0 release, the module was independently provided as a TORNDB package. Torndb is just a simple package for mysqldb and does not support Python 3.
2, TORNDB installation
Pip Install Torndb
3. Connection Initialization
Class Applicatin (tornado.web.application):
def Init(self):
Handler = [
(r "index/", Indexhandler),
]
settings = dic( template_path = os.path.join(os.path.dirname(__file__),"templates"), static_path = os.path.join(os.path.dirname(__file__),"static") ) super(Application,self).__init__(handler,**settings) self.db = torndb.Connection( host = "127,0,0,1", database = "ihome", user = "root", pwd = "mysql" )
4. Using the database
1. Execute the statement
1. Execute (query,*patameters,**kwparameters)
1. Query: SQL statement to execute
2, *patameters: For the parameters specified in the SQL statement data value 3, **kwparameters: For the parameters specified in the SQL statement data assignment Operation 4, return the effect of the last self-increment field value 2, Execu Te_rowcount (query, (patameters,**kwparameters)) 1, query: SQL statement to execute 2, *patameters: Data is passed to the parameter specified in the SQL statement 3, **kwparameters: The assignment of data to the parameters specified in the SQL statement 4, returns the number of rows affected 3, instance: Db.execute ("INSERT into houses (title, Position, Pric E, score, comments) values (%s,%s,%s,%s,%s), "individually decorated cottage", "close to Wen Jin Jie", 280, 5, 128) or Db.execute ("insert INTO Houses (title, position, price, score, comments) values (% (title) s,% (position) s,% (price) s,% (score) s,% (comments) s) ", Title= "individually decorated cottage", position= "close to Wen Jin Jie", price=280, Score=5, comments=128) 2, query statement 1, get (query, parameters, *kwparameters) 1. Query: SQL statement to execute 2, *patameters: Data value for the parameters specified in the SQL statement 3, **kwparameters: For the parameters specified in the SQL statement data assignment Operation 4, return a single-line knot Or none, if there are multiple lines then the error. The return value is torndb. The row type is a class Dictionary object that supports both the dictionary's keyword index and the object's zodiac access. 2. Query (query, parameters, *kwparameters) 1, query: SQL statement to execute 2, *pataMeters: Data is passed to the parameters specified in the SQL statement 3, **kwparameters: Assignment of data to the parameters specified in the SQL statement 4, returns the multiline result, torndb. The list of row.
Tornado-Database (TORNDB package)