Python development "modules": torndb

Source: Internet
Author: User

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

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.