<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 使用`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") ``` * * * * * *
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看