I. Overview of TORNDB
Torndb is a lightweight module based on the MYSQLDB package, which is part of the Tornado framework. Its Project homepage is: HTTPS://GITHUB.COM/BDARNELL/TORNDB. Since the tornado3.0 version, it has been released as a stand-alone module. Can be installed directly by means of Easy_install or PIP.
Second, connection and inquiry
1. Create a connection
Import Torndb
db = Torndb. Connection ("127.0.0.1:3306", "Test", user= "root", password= "admin")
Default character set UTF8, no need to add charset = "UTF8". Also note that its default time zone is time_zone= ' +0:00 ', which can be manually designated as the East 8 area when connected.
2, inquiry
All operations in MYSQLDB are executed through execute, and the TORNDB after TA encapsulation provides 3 kinds of execute,get,query. Example of execute:
The code is as follows |
Copy Code |
cre= ' CREATE TABLE blog (id int,content text) ' Db.execute (CRE) string= ' Wawuee ' exe= ' INSERT into blog (id,content) values (%d, "%s") '% (1,string) Db.execute (EXE) Execute includes creating tables, inserting tables, deleting tables, and so on, and separately encapsulates inserts, Insertmany, update, and Updatemany functions, and besides the general execute function, there are execute_lastrowid, Execute_rowcount, Executemany, Executemany_lastrowid, Executemany_rowcount functions. |
Query and get
All two are used to return data results, except that query can return more than one result, when the result of a get query is empty, returns none, and returns more than one result when an exception is reported, typically only for situations where a single result is returned.
The code is as follows |
Copy Code |
Query queries >>>sql = ' SELECT * FROM test WHERE name =%s and ID <%s ' >>>db.query (SQL, ' BBB ', 11) [{' Date ': None, ' id ': 1L, ' name ': U ' BBB '}, {' Date ': None, ' id ': 2L, ' name ': U ' BBB '}] Get query >>>sql = ' SELECT * FROM Test WHERE id =%s ' >>>db.get (SQL, 2) {' Date ': None, ' id ': 2L, ' name ': U ' BBB '} Query queries, the result is a list of multiple rows, and a single line for dictionaries only. |
Insert and Insertmany
The code is as follows |
Copy Code |
>>>sql = "INSERT into Test (id,name,date) VALUES (%s,%s,%s)" >>>db.insert (SQL, MB, "AAA", ' 0000-01-01 ') 100L The Insert parameter does not support a list or tuple, and you can use Insertmany if you want to insert a list or tuple #插入单行记录 sql = "INSERT into Test (id,name,date) VALUES (%s,%s,%s)" Db.insertmany (sql,[[200, ' BBB ', None]]) 200L #插入多行记录 Db.insertmany (sql,[[300, ' BBB ', none],[400, ' BBB ', None]]) 400L Db.insertmany (sql,[, ' BBB ', none), (401, ' BBB ', none)]) 401L |
Summary, Torndb MYSQLDB package, Query,get return is list,dict these, very convenient, can be used directly, this is TA's advantages, and is the default automatic commit, do not MYSQLDB manual commit, use up is very concise