24h購物| | PChome| 登入
2023-04-18 05:00:29| 人氣10| 回應0 | 上一篇 | 下一篇
推薦 0 收藏 0 轉貼0 訂閱站台

網站架設 Python 使用 MySQL Connector

1.jpg

網站架設介紹如何利用 Python 的 MySQL Connector 模組連接 MySQL/MariaDB 資料庫,進行查詢、新增或刪除等各類操作。



Python 有很多 MySQL/MariaDB 資料庫相幹的模組,而最常被利用的就是 MySQL Connector 與 MySQLdb 這兩個模組,以下是 MySQL Connector 模組的利用體式格局。
安裝 MySQL Connector 模組
開啟 Windows 中的號令提醒自元,利用 pip 安裝 Python 的 MySQL Connector 模組:

 

  1. pip install mysql-connector-python
複製代碼


安裝 Python 的 MySQL Connector 模組
安裝 Python 的 MySQL Connector 模組
安裝好以後,就可以開始利用了 Python 的 MySQL Connector 模組。

毗連資料庫
以下是利用 MySQL Connector 模組毗鄰資料庫的典範榜樣:

 

  1. import mysql.connector
  2. from mysql.connector import Error
  3.  
  4. try:
  5.     # 毗鄰 MySQL/MariaDB 資料庫
  6.     connection = mysql.connector.connect(
  7.         host='localhost',          # 主機名稱
  8.         database='officeguide_db', # 資料庫名稱
  9.         user='officeguide',        # 帳號
  10.         password='your_password')  # 暗碼
  11.  
  12.     if connection.is_connected():
  13.  
  14.         # 顯示資料庫版本
  15.         db_Info = connection.get_server_info()
  16.         print("資料庫版本:", db_Info)
  17.  
  18.         # 顯示今朝使用的資料庫
  19.         cursor = connection.cursor()
  20.         cursor.execute("SELECT DATABASE();")
  21.         record = cursor.fetchone()
  22.         print("今朝利用的資料庫:", record)
  23.  
  24. except Error as e:
  25.     print("資料庫連接失敗:", e)
  26.  
  27. finally:
  28.     if (connection.is_connected()):
  29.         cursor.close()
  30.         connection.close()
  31.         print("資料庫連線已封閉")
複製代碼


資料庫版本: 5.5.5-10.4.8-MariaDB
今朝利用的資料庫: ('officeguide_db',)
資料庫連線已封閉


查詢資料
以下是利用 SELECT 查詢資料的範例:

 

  1. import mysql.connector
  2. from mysql.connector import Error
  3.  
  4. try:
  5.     # 毗連 MySQL/MariaDB 資料庫
  6.     connection = mysql.connector.connect(
  7.         host='localhost',          # 主機名稱
  8.         database='officeguide_db', # 資料庫名稱
  9.         user='officeguide',        # 帳號
  10.         password='your_password')  # 暗碼
  11.  
  12.     # 查詢資料庫
  13.     cursor = connection.cursor()
  14.     cursor.execute("SELECT name, age FROM persons;")
  15.  
  16.     # 列出查詢的資料
  17.     for (name, age) in cursor:
  18.         print("Name: %s, Age: %d" % (name, age))
  19.  
  20.  
  21. except Error as e:
  22.     print("資料庫毗連失敗:", e)
  23.  
  24. finally:
  25.     if (connection.is_connected()):
  26.         cursor.close()
  27.         connection.close()
  28.         print("資料庫連線已封閉")
網站架設 複製代碼


Name: Arden, Age: 32
Name: Bond, Age: 54
Name: Cole, Age: 12
Name: Dana, Age: 19
資料庫連線已關閉

也能夠利用 fetchall 將資料一次掃數抓到 Python 中再處理:
 

  1. import mysql.connector
  2. from mysql.connector import Error
  3.  
  4. try:
  5.     # 毗連 MySQL/MariaDB 資料庫
  6.     connection = mysql.connector.connect(
  7.         host='localhost',          # 主機名稱
  8.         database='officeguide_db', # 資料庫名稱
  9.         user='officeguide',        # 帳號
  10.         password='your_password')  # 暗碼
  11.  
  12.     # 查詢資料庫
  13.     cursor = connection.cursor()
  14.     cursor.execute("SELECT name, age FROM persons;")
  15.  
  16.     # 取回悉數的資料
  17.     records = cursor.fetchall()
  18.     print("資料筆數:", cursor.rowcount)
  19.  
  20.     # 列出查詢的資料
  21.     for (name, age) in records:
  22.         print("Name: %s, Age: %d" % (name, age))
  23.  
  24. except Error as e:
  25.     print("資料庫毗連失敗:", e)
  26.  
  27. finally:
  28.     if (connection.is_connected()):
  29.         cursor.close()
  30.         connection.close()
  31.         print("資料庫連線已封閉")
複製代碼


資料筆數: 4
Name: Arden, Age: 32
Name: Bond, Age: 54
Name: Cole, Age: 12
Name: Dana, Age: 19
資料庫連線已關閉


新增資料
以下是利用 INSERT 新增資料的類型:

 

  1. import mysql.connector
  2. from mysql.connector import Error
  3.  
  4. try:
  5.     # 連接 MySQL/MariaDB 資料庫
  6.     connection = mysql.connector.connect(
  7.         host='localhost',          # 主機名稱
  8.         database='officeguide_db', # 資料庫名稱
  9.         user='officeguide',        # 帳號
  10.         password='your_password')  # 暗碼
  11.  
  12.     # 新增資料
  13.     sql = "INSERT INTO persons (name, age, city) VALUES (%s, %s, %s);"
  14.     new_data = ("Jack", 13, "Kaohsiung")
  15.     cursor = connection.cursor()
  16.     cursor.execute(sql, new_data)
  17.  
  18.     # 確認資料有存入資料庫
  19.     connection.commit()
  20.  
  21. except Error as e:
  22.     print("資料庫連接失敗:", e)
  23.  
  24. finally:
  25.     if (connection.is_connected()):
  26.         cursor.close()
  27.         connection.close()
複製代碼


點竄資料
以下是利用 UPDATE 更新資料的範例:

 

  1. import mysql.connector
  2. from mysql.connector import Error
  3.  
  4. try:
  5.     # 毗連 MySQL/MariaDB 資料庫
  6.     connection = mysql.connector.connect(
  7.         host='localhost',          # 主機名稱
  8.         database='officeguide_db', # 資料庫名稱
  9.         user='officeguide',        # 帳號
  10.         password='your_password')  # 暗碼
  11.  
  12.     # 更新資料
  13.     sql = "UPDATE persons SET age = %s WHERE id = %s;"
  14.     cursor = connection.cursor()
  15.     cursor.execute(sql, (27, 6))
  16.  
  17.     # 確認資料有存入資料庫
  18.     connection.commit()
  19.  
  20. except Error as e:
  21.     print("資料庫毗連失敗:", e)
  22.  
  23. finally:
  24.     if (connection.is_connected()):
  25.         cursor.close()
  26.         connection.close()
複製代碼


刪除資料
以下是利用 DELETE 刪除資料的規範:

 

  1. import mysql.connector
  2. from mysql.connector import Error
  3.  
  4. try:
  5.     # 連接 MySQL/MariaDB 資料庫
  6.     connection = mysql.connector.connect(
  7.         host='localhost',          # 主機名稱
  8.         database='officeguide_db', # 資料庫名稱
  9.         user='officeguide',        # 帳號
  10.         password='your_password')  # 暗碼
  11.  
  12.     # 更新資料
  13.     sql = "DELETE FROM persons WHERE id = %s;"
  14.     cursor = connection.cursor()
  15.     cursor.execute(sql, (6,))
  16.  
  17.     # 確認資料有存入資料庫
  18.     connection.commit()
  19.  
  20. except Error as e:
  21.     print("資料庫連接失敗:", e)
  22.  
  23. finally:
  24.     if (connection.is_connected()):
  25.         cursor.close()
  26.         connection.close()
複製代碼


參考資料:MySQL 官方文件、pynative

https://officeguide.cc/python-mysql-mariadb-database-connector-tutorial-examples/



本篇文章引用自此:

台長: julianmuq2
人氣(10) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 電視賞析(綜藝、戲劇、影集、節目) | 個人分類: NetYea |
此分類下一篇:Arduino ESP32 用光敏電阻做小夜燈
此分類上一篇:利用Highcharts實現柱狀圖、餅狀圖、曲線圖三圖合一

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文