<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之旅 廣告
                # PyMySQL 教程 > 原文: [http://zetcode.com/python/pymysql/](http://zetcode.com/python/pymysql/) PyMySQL 教程展示了如何使用 PyMySQL 模塊在 Python 中對 MySQL 進行編程。 ## PyMySQL PyMySQL 是基于 PEP 249 的純 Python MySQL 客戶端庫。大多數公共 API 與`mysqlclient`和`MySQLdb`兼容。 PyMySQL 可與 MySQL 5.5+和 MariaDB 5.5+ 一起使用。 MySQL 是領先的開源數據庫管理系統。 它是一個多用戶,多線程的數據庫管理系統。 MySQL 在網絡上特別流行。 ## PyMySQL 安裝 ```py $ sudo pip3 install PyMySQL ``` 我們使用`pip3`工具安裝 PyMySQL。 ## PyMySQL 版本示例 在下面的示例中,我們獲取 MySQL 的版本。 `version.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- import pymysql con = pymysql.connect('localhost', 'user17', 's$cret', 'mydb') with con: cur = con.cursor() cur.execute("SELECT VERSION()") version = cur.fetchone() print("Database version: {}".format(version[0])) ``` 在 MySQL 中,我們可以使用`SELECT VERSION()`獲取 MySQL 的版本。 ```py import pymysql ``` 我們導入`pymysql`模塊。 ```py con = pymysql.connect('localhost', 'user17', 's$cret', 'mydb') ``` 我們使用`connect()`連接到數據庫。 我們傳遞四個參數:主機名,MySQL 用戶名,密碼和數據庫名。 ```py with con: ``` 使用`with`關鍵字,Python 解釋器會自動釋放資源。 它還提供錯誤處理。 ```py cur = con.cursor() ``` 從連接對象,我們創建一個游標。 游標用于遍歷結果集中的記錄。 ```py cur.execute("SELECT VERSION()") ``` 我們調用游標的`execute()`方法并執行 SQL 語句。 ```py version = cur.fetchone() ``` `fetchone()`方法獲取查詢結果集的下一行,返回單個序列,或者在沒有更多數據可用時返回`None`。 ```py print("Database version: {}".format(version[0])) ``` 我們打印數據庫的版本。 ```py $ ./version.py Database version: 5.7.23-0ubuntu0.16.04.1 ``` 這是輸出。 ## PyMySQL `fetchAll` `fetchAll()`方法檢索查詢結果的所有(剩余)行,并將它們作為序列序列返回。 `retrieve_all.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- import pymysql con = pymysql.connect('localhost', 'user17', 's$cret', 'testdb') with con: cur = con.cursor() cur.execute("SELECT * FROM cities") rows = cur.fetchall() for row in rows: print("{0} {1} {2}".format(row[0], row[1], row[2])) ``` 在示例中,我們從數據庫表中檢索所有城市。 ```py cur.execute("SELECT * FROM cities") ``` 該 SQL 語句從`citys`表中選擇所有數據。 ```py rows = cur.fetchall() ``` `fetchall()`方法獲取所有記錄。 它返回一個結果集。 從技術上講,它是一個元組的元組。 每個內部元組代表表中的一行。 ```py for row in rows: print("{0} {1} {2}".format(row[0], row[1], row[2])) ``` 我們將數據逐行打印到控制臺。 ```py $ ./retrieve_all.py 1 Bratislava 432000 2 Budapest 1759000 3 Prague 1280000 4 Warsaw 1748000 5 Los Angeles 3971000 6 New York 8550000 7 Edinburgh 464000 8 Berlin 3671000 ``` 這是輸出。 ## PyMySQL 字典游標 默認游標以元組的元組返回數據。 當我們使用字典游標時,數據以 Python 字典的形式發送。 這樣,我們可以通過列名稱來引用數據。 `dictionary_cursor.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- import pymysql import pymysql.cursors con = pymysql.connect(host='localhost', user='user17', password='s$cret', db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) with con: cur = con.cursor() cur.execute("SELECT * FROM cities") rows = cur.fetchall() for row in rows: print(row["id"], row["name"]) ``` 在此示例中,我們使用字典游標獲取`citys`表的第一行。 ```py con = pymysql.connect(host='localhost', user='user17', password='s$cret', db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) ``` 在`connect()`方法中,我們將`pymysql.cursors.DictCursor`值傳遞給`cursorclass`參數。 ```py for row in rows: print(row["id"], row["name"]) ``` 我們通過`cities`表的列名稱引用數據。 ## PyMySQL 列標題 接下來,我們將展示如何使用數據庫表中的數據打印列標題。 `column_headers.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- import pymysql con = pymysql.connect('localhost', 'user17', 's$cret', 'testdb') with con: cur = con.cursor() cur.execute("SELECT * FROM cities") rows = cur.fetchall() desc = cur.description print("{0:>3} {1:>10}".format(desc[0][0], desc[1][0])) for row in rows: print("{0:3} {1:>10}".format(row[0], row[2])) ``` 列名稱被認為是元數據。 它們是從游標對象獲得的。 ```py desc = cur.description ``` 游標的`description`屬性返回有關查詢的每個結果列的信息。 ```py print("{0:>3} {1:>10}".format(desc[0][0], desc[1][0])) ``` 在這里,我們打印并格式化表格的列名。 ```py for row in rows: print("{0:3} {1:>10}".format(row[0], row[2])) ``` 我們遍歷并打印數據。 ```py $ ./column_headers.py id name 1 432000 2 1759000 3 1280000 4 1748000 5 3971000 6 8550000 7 464000 8 3671000 ``` 這是輸出。 ## PyMySQL 預備語句 在編寫預備語句時,我們使用占位符,而不是直接將值寫入語句中。 預準備的語句可提高安全性和性能。 `prepared_statement.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- import pymysql con = pymysql.connect('localhost', 'user17', 's$cret', 'testdb') # user input myid = 4 with con: cur = con.cursor() cur.execute("SELECT * FROM cities WHERE id=%s", myid) cid, name, population = cur.fetchone() print(cid, name, population) ``` 在示例中,我們獲得具有指定 ID 的行。 ```py cur.execute("SELECT * FROM cities WHERE id=%s", myid) ``` 我們使用由`%s`標記標識的占位符。 在執行 SQL 語句之前,將值綁定到它們的占位符。 ```py $ ./prepared_statement.py 4 Warsaw 1748000 ``` 這是輸出。 ## PyMySQL 受影響的行 `rowcount`是只讀的游標屬性,它指定最后一條`SELECT`,`UPDATE`或`INSERT`語句產生的行數。 `affected_rows.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- import pymysql con = pymysql.connect('localhost', 'user17', 's$cret', 'mydb') with con: cur = con.cursor() cur.execute("SELECT * FROM cities WHERE id IN (1, 2, 3)") # rows = cur.fetchall() # for row in rows: # print("{0} {1} {2}".format(row[0], row[1], row[2])) print("The query affected {} rows".format(cur.rowcount)) ``` 在示例中,我們有一條`SELECT`語句選擇三行。 ```py print("The query affected {} rows".format(cur.rowcount)) ``` 我們生成一條消息,顯示受影響的行數。 ```py $ ./affected_rows.py The query affected 3 rows ``` 這是輸出。 在本教程中,我們一直在使用 PyMySQL 模塊在 Python 中編寫 MySQL 數據庫。 您可能也對以下相關教程感興趣: [Python SQLite 教程](/python/sqlite/), [Python PostgreSQL 教程](/python/psycopg2/), [PyMongo 教程](/python/pymongo/), [Peewee 教程](/python/peewee/), [pyDAL 教程](/python/pydal/)和 [Python 教程](/lang/python/),或列出 [Python 教程](/all/#python)。
                  <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>

                              哎呀哎呀视频在线观看