<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 處理錯誤 > 原文: [https://thepythonguru.com/handling-errors/](https://thepythonguru.com/handling-errors/) * * * 于 2020 年 1 月 7 日更新 * * * 與數據庫交互是一個容易出錯的過程,因此我們必須始終實現某種機制來優雅地處理錯誤。 MySQLdb 具有`MySQLdb.Error`異常,這是一個頂級異常,可用于捕獲`MySQLdb`模塊引發的所有數據庫異常。 ```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" number_of_rows = cursor.execute(sql) print(number_of_rows) db.close() except my.Error as e: print(e) except : print("Unknown error occurred") ``` ## MySQLdb 中的兩個主要錯誤 * * * 需要注意的是,MySQLdb 中有兩類異常類: 1. `DatabaseError` 2. `InterfaceError` * * * 1. **`DatabaseError`**:當數據處理中存在問題,sql 語法錯誤,mysql 內部問題時,引發此異常。 如果建立連接并且出現問題,則`DatabaseError`會捕獲到它。 2. **`InterfaceError`**:當由于某種原因數據庫連接失敗時,MySQLdb 將引發`InterfaceError`。 注意`InterfaceError`僅在與數據庫連接存在內部問題時才引發,MySQLdb 不會因錯誤的數據庫名稱或密碼而引發`InterfaceError`。 `DatabaseError`進一步分為 6 種類型: 1. `DataError` 2. `InternalError` 3. `IntegrityError` 4. `OperationalError` 5. `NotSupportedError` 6. `ProgrammingError` * * * 1. **`DataError`**:當數據處理出現問題時,例如除以零,范圍的數值,MySQLdb 會引發此錯誤。 2. **`InternalError`**:當 MySQL 數據庫本身存在一些內部錯誤時,引發此異常。 例如無效的游標,事務不同步等。 3. **`IntegrityError`**:當外鍵檢查失敗時,引發此異常。 4. **`OperationalError`**:對于不受程序員控制的事情,會引發此異常。 例如,意外斷開連接,內存分配錯誤等,所選數據庫不存在。 5. **`NotSupportedError`**:當存在不支持的方法或 api 時,引發此異常。 6. **`ProgrammingError`**:引發此編程錯誤。 例如找不到表,mysql 語法錯誤,指定的參數數量錯誤等。 ```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" number_of_rows = cursor.execute(sql) print(number_of_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") ``` 在下一篇文章中,我們討論[如何從數據庫](/fetching-records-using-fetchone-and-fetchmany/)中獲取特定的行數。 * * * * * *
                  <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>

                              哎呀哎呀视频在线观看