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