About the Fetchone () function and the Fetchall () function (RPM) used in querying database content in Python

Source: Internet
Author: User

Recently, when working with the MySQL database in Python, the following two functions were encountered, labeled:

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] can be accessed sequentially Host,user,password

Fetchall () Usage:

Cur.execute ("SELECT * from User")

If the select itself takes more than one data:

Cursor.fetchone (): will take only the top first result, return 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 two-dimensional tuples, 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: The ID and title are specific content

Python in MySQL when using Fetchall or Fetchone, together, Fetchall returns a two-dimensional tuple (the tuple contains tuples), Fetchone only returns one-dimensional tuple.

About the Fetchone () function and the Fetchall () function (RPM) used in querying database content in Python

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.