Database query Operations (FETCHONE,FETCHALL)

Source: Internet
Author: User

database Query Operations

Python queries MySQL uses the Fetchone () method to get a single piece of data, using the Fetchall () method to get multiple data.

    Fetchone (): This method gets the next query result set. The result set is an object    fetchall (): Receives all the returned result rows.    RowCount: This is a read-only property and returns the number of rows affected after the Execute () method is executed.

Fetchone ():

Returns a single tuple, which is a record (row), and returns none if no result is found

Fetchall ():

Returns multiple tuples, which returns multiple records (rows) and returns if no result ()

Note: null in MySQL, and none in Python

the usage is as follows: Fetchone () Usage: Cur.execute ("Select Host,user,password from user where user= '%s '"%ACC) Jilu= Cur.fetchone ()##此时 via jilu[0],jilu[1],jilu[2] to access Host,user,passwordfetchall () Usage: Cur.execute ("SELECT * from user"If the select itself takes more than one piece of data: Cursor.fetchone (): Takes only the first result of the top and returns a single tuple such as ('ID','title'), and then use Cursor.fetchone () multiple times to get the next result until it is empty. Cursor.fetchall (): Returns all results, returning a two-dimensional tuple, such as ('ID','title'),('ID','title') , if the select itself takes only one piece of data: Cursor.fetchone (): Returns only one result, returning a single tuple such as ('ID','title'). Cursor.fetchall (): will also return all results, returning two-dimensional tuples, such as (('ID','title'), note: Where the ID and title are specific to the content of Python in MySQL when using Fetchall or Fetchone, together, Fetchall returns a two-dimensional tuple (tuples containing tuples), Fetchone only returns one-dimensional tuples. 

 

Database query Operations (FETCHONE,FETCHALL)

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.