# 使用`fetchone()`和`fetchmany()`獲取記錄
> 原文: [https://thepythonguru.com/fetching-records-using-fetchone-and-fetchmany/](https://thepythonguru.com/fetching-records-using-fetchone-and-fetchmany/)
* * *
于 2020 年 1 月 7 日更新
* * *
到目前為止,我們一直在使用游標對象的`fetchall()`方法來獲取記錄。 一次性訪問所有記錄的過程并非十分有效。 結果,MySQLdb 具有游標對象的`fetchone()`和`fetchmany()`方法來更有效地獲取記錄。
| 方法 | 描述 |
| --- | --- |
| `fetchone()` | 此方法以元組形式返回一個記錄,如果沒有更多記錄,則返回`None`。 |
| `fetchmany(number_of_records)` | 此方法接受要提取的記錄數,并返回元組,其中每個記錄本身就是一個元組。 如果沒有更多記錄,則返回一個空元組。 |
## 使用`fetchone()`
* * *
```py
from __future__ import print_function
import MySQLdb as my
try:
db = my.connect(host="127.0.0.1",
user="root",
passwd="",
db="world"
)
cursor = db.cursor()
sql = "select * from city where id < 10"
number_of_rows = cursor.execute(sql)
print(cursor.fetchone()) # fetch the first row only
db.close()
except my.DataError as e:
print("DataError")
print(e)
except my.InternalError as e:
print("InternalError")
print(e)
except my.IntegrityError as e:
print("IntegrityError")
print(e)
except my.OperationalError as e:
print("OperationalError")
print(e)
except my.NotSupportedError as e:
print("NotSupportedError")
print(e)
except my.ProgrammingError as e:
print("ProgrammingError")
print(e)
except :
print("Unknown error occurred")
```
## 使用`fetchone()`遍歷結果
* * *
```py
from __future__ import print_function
import MySQLdb as my
try:
db = my.connect(host="127.0.0.1",
user="root",
passwd="",
db="world"
)
cursor = db.cursor()
sql = "select * from city where id < 10"
number_of_rows = cursor.execute(sql)
while True:
row = cursor.fetchone()
if row == None:
break
print(row)
db.close()
except my.DataError as e:
print("DataError")
print(e)
except my.InternalError as e:
print("InternalError")
print(e)
except my.IntegrityError as e:
print("IntegrityError")
print(e)
except my.OperationalError as e:
print("OperationalError")
print(e)
except my.NotSupportedError as e:
print("NotSupportedError")
print(e)
except my.ProgrammingError as e:
print("ProgrammingError")
print(e)
except :
print("Unknown error occurred")
```
## 使用`fetchmany()`
* * *
```py
from __future__ import print_function
import MySQLdb as my
try:
db = my.connect(host="127.0.0.1",
user="root",
passwd="",
db="world"
)
cursor = db.cursor()
sql = "select * from city where id < 10"
number_of_rows = cursor.execute(sql)
print(cursor.fetchmany(2)) # fetch first 2 rows only
db.close()
except my.DataError as e:
print("DataError")
print(e)
except my.InternalError as e:
print("InternalError")
print(e)
except my.IntegrityError as e:
print("IntegrityError")
print(e)
except my.OperationalError as e:
print("OperationalError")
print(e)
except my.NotSupportedError as e:
print("NotSupportedError")
print(e)
except my.ProgrammingError as e:
print("ProgrammingError")
print(e)
except :
print("Unknown error occurred")
```
## 使用`fetchmany()`遍歷結果
* * *
```py
from __future__ import print_function
import MySQLdb as my
try:
db = my.connect(host="127.0.0.1",
user="root",
passwd="",
db="world"
)
cursor = db.cursor()
sql = "select * from city where id < 10"
number_of_rows = cursor.execute(sql)
while True:
two_rows = cursor.fetchmany(2)
if not two_rows:
break
print(two_rows)
db.close()
except my.DataError as e:
print("DataError")
print(e)
except my.InternalError as e:
print("InternalError")
print(e)
except my.IntegrityError as e:
print("IntegrityError")
print(e)
except my.OperationalError as e:
print("OperationalError")
print(e)
except my.NotSupportedError as e:
print("NotSupportedError")
print(e)
except my.ProgrammingError as e:
print("ProgrammingError")
print(e)
except :
print("Unknown error occurred")
```
* * *
* * *
- 初級 Python
- python 入門
- 安裝 Python3
- 運行 python 程序
- 數據類型和變量
- Python 數字
- Python 字符串
- Python 列表
- Python 字典
- Python 元組
- 數據類型轉換
- Python 控制語句
- Python 函數
- Python 循環
- Python 數學函數
- Python 生成隨機數
- Python 文件處理
- Python 對象和類
- Python 運算符重載
- Python 繼承與多態
- Python 異常處理
- Python 模塊
- 高級 Python
- Python *args和**kwargs
- Python 生成器
- Python 正則表達式
- 使用 PIP 在 python 中安裝包
- Python virtualenv指南
- Python 遞歸函數
- __name__ == "__main__"是什么?
- Python Lambda 函數
- Python 字符串格式化
- Python 內置函數和方法
- Python abs()函數
- Python bin()函數
- Python id()函數
- Python map()函數
- Python zip()函數
- Python filter()函數
- Python reduce()函數
- Python sorted()函數
- Python enumerate()函數
- Python reversed()函數
- Python range()函數
- Python sum()函數
- Python max()函數
- Python min()函數
- Python eval()函數
- Python len()函數
- Python ord()函數
- Python chr()函數
- Python any()函數
- Python all()函數
- Python globals()函數
- Python locals()函數
- 數據庫訪問
- 安裝 Python MySQLdb
- 連接到數據庫
- MySQLdb 獲取結果
- 插入行
- 處理錯誤
- 使用fetchone()和fetchmany()獲取記錄
- 常見做法
- Python:如何讀取和寫入文件
- Python:如何讀取和寫入 CSV 文件
- 用 Python 讀寫 JSON
- 用 Python 轉儲對象