Python development [module]: torndb, python development torndb

Source: Internet
Author: User

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

  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.