torndb Module
Summary: 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 standalone module. Can be installed directly via Easy_install or PIP.
1. Connect to the database
# Create connection Import torndb# database, account number, password, time zone db = Torndb. Connection ("192.168.1.134:3306", ' Jefrey ', user= ' Lzl ', password= ' 123456 ', time_zone= ')
Add:
- Default character set UTF8, no need to add charset = "UTF8"
- It is also important to note that the default time zone is time_zone= ' +0:00 '
- This can be manually designated as the East 8 zone at the time of connection.
2. Create a data table
# Create create= ' CREATE TABLE blog (id int,content text) ' Db.execute (Create)
3. Inserting data
①, execute
# Insert execute# execute= ' INSERT INTO blog (id,content) values (%d, "%s") '% (7, ' Jefrey5 ') # Db.execute (execute)
②, insert
# single Insertinsert = ' INSERT into blog (id,content) VALUES (%s,%s) ' Db.insert (insert,10, ' Jefrey10 ')
③, Insertmany
# insertmanyinsertmany = ' INSERT into blog (id,content) VALUES (%s,%s) ' Db.insertmany (insert,[[11, ' Jefrey11 '],[12, ' Jefrey12 '])
4. Query data
①, query queries
# Query sql = ' SELECT * FROM blog WHERE content =%s and Id <%s ' # query to a data list for database = Db.query (sql, ' Jefrey ', 11) # [{' Content ': U ' Jefrey ', ' ID ': 1l}]# query to multiple data for also list DATABASE5 = db.query (sql, ' Jefrey5 ', one-by-one) # [{' Content ': U ' Jefrey5 ', ' ID ': 5 L}, {' content ': U ' Jefrey5 ', ' ID ': 8L}, {' content ': U ' Jefrey5 ', ' ID ': 7l}]# query data does not exist empty list database10 = db.query (sql, ' Jefrey1 0 ', 11) # []
②, get query
# get Query sql = ' SELECT * FROM blog WHERE content =%s and Id <%s ' # query to a data dictionary = db.get (sql, ' Jefrey ', one) # {' Co Ntent ': U ' Jefrey ', ' ID ': 1l}# query to multiple data times wrong DATABASE5 = db.get (sql, ' Jefrey5 ', one) # Exception:multiple rows returned for data Base.get () query# query data does not exist for Nonedatabase10 = Db.get (sql, ' Jefrey10 ', one) # None
Python development module: TORNDB