最近中文字幕国语免费完整,中文亚洲无线码49vv,中文无码热在线视频,亚洲自偷自拍熟女另类,中文字幕高清av在线

當(dāng)前位置: 首頁(yè) > 開(kāi)發(fā)者資訊

Python 怎么連接 MySQL 數(shù)據(jù)庫(kù)?Python 與 MySQL 數(shù)據(jù)庫(kù)交互步驟

  在Python中連接MySQL數(shù)據(jù)庫(kù)是實(shí)現(xiàn)數(shù)據(jù)交互的重要步驟。通過(guò)Python與MySQL數(shù)據(jù)庫(kù)的連接,可以實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的查詢、插入、更新和刪除等操作。以下是詳細(xì)的連接步驟和相關(guān)說(shuō)明。

  一、安裝必要的庫(kù)

  在使用Python連接MySQL數(shù)據(jù)庫(kù)之前,需要安裝一個(gè)數(shù)據(jù)庫(kù)連接器。常用的庫(kù)有 mysql-connector-python 和 pymysql。可以通過(guò)以下命令進(jìn)行安裝:

  pip install mysql-connector-python

  運(yùn)行

  或者:

  pip install pymysql

  運(yùn)行

  二、連接MySQL數(shù)據(jù)庫(kù)

  1. 使用 mysql-connector-python 連接

  import mysql.connector

  # 建立數(shù)據(jù)庫(kù)連接

  mydb = mysql.connector.connect(

  host="localhost", # 數(shù)據(jù)庫(kù)主機(jī)名

  user="yourusername", # 數(shù)據(jù)庫(kù)用戶名

  password="yourpassword", # 數(shù)據(jù)庫(kù)密碼

  database="yourdatabase" # 數(shù)據(jù)庫(kù)名稱

  )

  # 檢查連接是否成功

  if mydb.is_connected():

  print("Connected to MySQL database")

  運(yùn)行

  2. 使用 pymysql 連接

  import pymysql

  # 建立數(shù)據(jù)庫(kù)連接

  conn = pymysql.connect(

  host='localhost', # 數(shù)據(jù)庫(kù)主機(jī)名

  user='root', # 數(shù)據(jù)庫(kù)用戶名

  password='1234567', # 數(shù)據(jù)庫(kù)密碼

  database='netbuy', # 數(shù)據(jù)庫(kù)名稱

  port=3306, # 數(shù)據(jù)庫(kù)端口

  charset='utf8' # 字符集

  )

  # 檢查連接是否成功

  if conn.open:

  print("Connected to MySQL database")

  運(yùn)行

360截圖20250426224640574.jpg

  三、創(chuàng)建游標(biāo)對(duì)象

  連接成功后,需要?jiǎng)?chuàng)建一個(gè)游標(biāo)對(duì)象,用于執(zhí)行SQL查詢和獲取結(jié)果。

  # 創(chuàng)建游標(biāo)對(duì)象

  cursor = mydb.cursor() # 使用 mysql-connector-python

  # cursor = conn.cursor() # 使用 pymysql

  運(yùn)行

  四、執(zhí)行SQL查詢

  通過(guò)游標(biāo)對(duì)象的 execute() 方法執(zhí)行SQL查詢,并獲取結(jié)果。

  1. 查詢數(shù)據(jù)

  # 執(zhí)行查詢

  cursor.execute("SELECT * FROM your_table")

  # 獲取所有結(jié)果

  result = cursor.fetchall()

  for row in result:

  print(row)

  運(yùn)行

  2. 插入數(shù)據(jù)

  # 插入數(shù)據(jù)

  sql = "insert INTO your_table (column1, column2) VALUES (%s, %s)"

  val = ("value1", "value2")

  cursor.execute(sql, val)

  mydb.commit() # 提交更改

  print("記錄插入成功")

  運(yùn)行

  3. 更新數(shù)據(jù)

  # 更新數(shù)據(jù)

  sql = "UPDATE your_table SET column1 = %s WHERE column2 = %s"

  val = ("new_value", "old_value")

  cursor.execute(sql, val)

  mydb.commit()

  print("記錄更新成功")

  運(yùn)行

  4. 刪除數(shù)據(jù)

  # 刪除數(shù)據(jù)

  sql = "delete FROM your_table WHERE column1 = %s"

  val = ("value1",)

  cursor.execute(sql)

  mydb.commit()

  print("記錄刪除成功")

  運(yùn)行

  五、關(guān)閉連接

  完成數(shù)據(jù)庫(kù)操作后,需要關(guān)閉游標(biāo)和連接,以釋放資源。

  # 關(guān)閉游標(biāo)

  cursor.close()

  # 關(guān)閉連接

  mydb.close() # 使用 mysql-connector-python

  # conn.close() # 使用 pymysql

  運(yùn)行

  六、處理異常

  在連接和操作數(shù)據(jù)庫(kù)時(shí),可能會(huì)遇到各種異常。使用 try-except-finally 塊可以更好地處理這些異常。

  try:

  # 建立連接

  mydb = mysql.connector.connect(...)

  # 創(chuàng)建游標(biāo)

  cursor = mydb.cursor()

  # 執(zhí)行查詢

  cursor.execute("SELECT * FROM your_table")

  # 獲取結(jié)果

  result = cursor.fetchall()

  for row in result:

  print(row)

  except mysql.connector.Error as e:

  print("數(shù)據(jù)庫(kù)操作失敗:", e)

  finally:

  # 關(guān)閉游標(biāo)

  if 'cursor' in locals():

  cursor.close()

  # 關(guān)閉連接

  if 'mydb' in locals():

  mydb.close()

  通過(guò)上述步驟,可以使用Python連接MySQL數(shù)據(jù)庫(kù),并執(zhí)行各種數(shù)據(jù)庫(kù)操作。選擇合適的庫(kù)(如 mysql-connector-python 或 pymysql)可以根據(jù)具體需求和環(huán)境進(jìn)行調(diào)整。同時(shí),注意處理異常和關(guān)閉連接,以確保程序的穩(wěn)定性和安全性。

 


猜你喜歡