使用fetchone()fetchmany()获取记录

原文: https://thepythonguru.com/fetching-records-using-fetchone-and-fetchmany/


于 2020 年 1 月 7 日更新


到目前为止,我们一直在使用游标对象的fetchall()方法来获取记录。 一次性访问所有记录的过程并非十分有效。 结果,MySQLdb 具有游标对象的fetchone()fetchmany()方法来更有效地获取记录。

方法 描述
fetchone() 此方法以元组形式返回一个记录,如果没有更多记录,则返回None
fetchmany(number_of_records) 此方法接受要提取的记录数,并返回元组,其中每个记录本身就是一个元组。 如果没有更多记录,则返回一个空元组。

使用fetchone()


  1. from __future__ import print_function
  2. import MySQLdb as my
  3. try:
  4. db = my.connect(host="127.0.0.1",
  5. user="root",
  6. passwd="",
  7. db="world"
  8. )
  9. cursor = db.cursor()
  10. sql = "select * from city where id < 10"
  11. number_of_rows = cursor.execute(sql)
  12. print(cursor.fetchone()) # fetch the first row only
  13. db.close()
  14. except my.DataError as e:
  15. print("DataError")
  16. print(e)
  17. except my.InternalError as e:
  18. print("InternalError")
  19. print(e)
  20. except my.IntegrityError as e:
  21. print("IntegrityError")
  22. print(e)
  23. except my.OperationalError as e:
  24. print("OperationalError")
  25. print(e)
  26. except my.NotSupportedError as e:
  27. print("NotSupportedError")
  28. print(e)
  29. except my.ProgrammingError as e:
  30. print("ProgrammingError")
  31. print(e)
  32. except :
  33. print("Unknown error occurred")

使用fetchone()遍历结果


  1. from __future__ import print_function
  2. import MySQLdb as my
  3. try:
  4. db = my.connect(host="127.0.0.1",
  5. user="root",
  6. passwd="",
  7. db="world"
  8. )
  9. cursor = db.cursor()
  10. sql = "select * from city where id < 10"
  11. number_of_rows = cursor.execute(sql)
  12. while True:
  13. row = cursor.fetchone()
  14. if row == None:
  15. break
  16. print(row)
  17. db.close()
  18. except my.DataError as e:
  19. print("DataError")
  20. print(e)
  21. except my.InternalError as e:
  22. print("InternalError")
  23. print(e)
  24. except my.IntegrityError as e:
  25. print("IntegrityError")
  26. print(e)
  27. except my.OperationalError as e:
  28. print("OperationalError")
  29. print(e)
  30. except my.NotSupportedError as e:
  31. print("NotSupportedError")
  32. print(e)
  33. except my.ProgrammingError as e:
  34. print("ProgrammingError")
  35. print(e)
  36. except :
  37. print("Unknown error occurred")

使用fetchmany()


  1. from __future__ import print_function
  2. import MySQLdb as my
  3. try:
  4. db = my.connect(host="127.0.0.1",
  5. user="root",
  6. passwd="",
  7. db="world"
  8. )
  9. cursor = db.cursor()
  10. sql = "select * from city where id < 10"
  11. number_of_rows = cursor.execute(sql)
  12. print(cursor.fetchmany(2)) # fetch first 2 rows only
  13. db.close()
  14. except my.DataError as e:
  15. print("DataError")
  16. print(e)
  17. except my.InternalError as e:
  18. print("InternalError")
  19. print(e)
  20. except my.IntegrityError as e:
  21. print("IntegrityError")
  22. print(e)
  23. except my.OperationalError as e:
  24. print("OperationalError")
  25. print(e)
  26. except my.NotSupportedError as e:
  27. print("NotSupportedError")
  28. print(e)
  29. except my.ProgrammingError as e:
  30. print("ProgrammingError")
  31. print(e)
  32. except :
  33. print("Unknown error occurred")

使用fetchmany()遍历结果


  1. from __future__ import print_function
  2. import MySQLdb as my
  3. try:
  4. db = my.connect(host="127.0.0.1",
  5. user="root",
  6. passwd="",
  7. db="world"
  8. )
  9. cursor = db.cursor()
  10. sql = "select * from city where id < 10"
  11. number_of_rows = cursor.execute(sql)
  12. while True:
  13. two_rows = cursor.fetchmany(2)
  14. if not two_rows:
  15. break
  16. print(two_rows)
  17. db.close()
  18. except my.DataError as e:
  19. print("DataError")
  20. print(e)
  21. except my.InternalError as e:
  22. print("InternalError")
  23. print(e)
  24. except my.IntegrityError as e:
  25. print("IntegrityError")
  26. print(e)
  27. except my.OperationalError as e:
  28. print("OperationalError")
  29. print(e)
  30. except my.NotSupportedError as e:
  31. print("NotSupportedError")
  32. print(e)
  33. except my.ProgrammingError as e:
  34. print("ProgrammingError")
  35. print(e)
  36. except :
  37. print("Unknown error occurred")