Python development [module]: torndb, python development torndb
Torndb Module
Summary: torndb is a lightweight module encapsulated based on MySQLdb and is part of the tornado framework. Its project home page is: https://github.com/bdarnell/torndb. Tornado3.0 and later versions have been released as an independent module. You can install the SDK directly using easy_install or pip.
1. Connect to the database
# Create a connection to import torndb # Database, account, password, and Time Zone db = torndb. connection ("192.168.1.134: 3306", 'jefrey', user = 'lzl', password = '000000', time_zone = '+ ')
Supplement:
- The default Character Set UTF8 does not need to be added with charset = "utf8"
- Note that the default time zone is time_zone = '+'
- This can be manually specified as the East 8 zone during connection.
2. Create a data table
# Create = 'create table blog (id int, content text='db.exe cute (create)
3. insert data
① Execute
# Insert execute # execute = 'insert into blog (id, content) values (% d, "% s") '% (7, 'jefrey5') # db.exe cute (execute)
② Insert
# Single insertinsert = 'insert into blog (id, content) values (% s, % s) 'db. insert (insert, 10, 'jefrey10 ')
③ Insertworkflow
# insertmanyinsertmany = 'insert into blog(id,content)values(%s,%s)'db.insertmany(insert,[[11,'Jefrey11'],[12,'Jefrey12']])
4. query data
① Query
# Query SQL = 'select * FROM blog WHERE content = % s AND id <% s' # list database = db when a piece of data is queried. query (SQL, 'jefrey', 11) # [{'content': u'jefrey', 'id ': 1L}] # multiple pieces of data are also listed as database5 = db. query (SQL, 'jefrey5', 11) # [{'content': u'jefrey5', 'id': 5L}, {'content': u'jefrey5 ', 'id': 8L}, {'content': u'jefrey5', 'id': 7L}] # The Database 10 = db is empty when no data exists. query (SQL, 'jefrey10', 11) # []
② Get Query
# Get query SQL = 'select * FROM blog WHERE content = % s AND id <% s' # dictionary database = db when a piece of data is queried. get (SQL, 'jefrey', 11) # {'content': u'jefrey', 'id': 1L} # An error is returned when multiple data entries are queried. database5 = db. get (SQL, 'jefrey5', 11) # Exception: Multiple rows returned for Database. get () query # Nonedatabase10 = db when the query data does not exist. get (SQL, 'jefrey10', 11) # None