Python x MySql

Python x MySql

更新於 發佈於 閱讀時間約 11 分鐘
pip install pymysql

原碼:https://reurl.cc/bGkxKr

資料庫連線

import pymysql
# 資料庫設定
db_settings = {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "資料庫管理員密碼",
"db": "test2022",
"charset": "utf8"
}
try:
# 建立Connection物件
conn = pymysql.connect(**db_settings)
except Exception as ex:
print(ex)

資料庫的連線建立完成後,要進行相關的操作,需要建立Cursor(指標)物件來執行,這邊使用Python的with陳述式,當資料庫存取完成後,自動釋放連線。

import pymysql

# 資料庫設定
db_settings = {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "資料庫管理員密碼",
"db": "test2022",
"charset": "utf8"
}
try:
# 建立Connection物件
conn = pymysql.connect(**db_settings)
# 建立Cursor物件
with conn.cursor() as cursor:
#資料表相關操作
print("ok")
except Exception as ex:
print(ex)

INSERT

import pymysql

# 資料庫設定
db_settings = {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "資料庫管理員密碼",
"db": "test2022",
"charset": "utf8"
}
try:
# 建立Connection物件
conn = pymysql.connect(**db_settings)
# 建立Cursor物件
with conn.cursor() as cursor:
#資料表相關操作
# 新增資料SQL語法
command = "INSERT INTO member(m_name, m_mail)VALUES(%s, %s)"
cursor.execute(command, ("test", "1234@cc"))
# 儲存變更
conn.commit()
except Exception as ex:
print(ex)

SELECT

import pymysql

# 資料庫設定
db_settings = {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
  "password": "資料庫管理員密碼",
"db": "test2022",
"charset": "utf8"
}
try:
# 建立Connection物件
conn = pymysql.connect(**db_settings)
# 建立Cursor物件
with conn.cursor() as cursor:
# 查詢資料SQL語法
command = "SELECT * FROM member"
# 執行指令
cursor.execute(command)
# 取得所有資料
result = cursor.fetchall()
print(result)
except Exception as ex:
print(ex)

Select 取得單筆資料 fetchone()

import pymysql

# 資料庫設定
db_settings = {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "資料庫管理員密碼",
"db": "test2022",
"charset": "utf8"
}
try:
# 建立Connection物件
conn = pymysql.connect(**db_settings)

# 建立Cursor物件
with conn.cursor() as cursor:
# 新增資料指令
command = "SELECT * FROM member"
# 執行指令
cursor.execute(command)
# 取得第一筆資料
#result = cursor.fetchone()
# 取得前五筆資料
result = cursor.fetchmany(5)
print(result)
except Exception as ex:
print(ex)

Select 取得篩選資料 where

import pymysql

# 資料庫設定
db_settings = {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "資料庫管理員密碼",
"db": "test2022",
"charset": "utf8"
}
try:
# 建立Connection物件
conn = pymysql.connect(**db_settings)

# 建立Cursor物件
with conn.cursor() as cursor:
# 新增資料指令
command = "SELECT * FROM member WHERE m_no = %s"
# 執行指令
cursor.execute(command, ("10",))
# 取得所有資料
result = cursor.fetchall()
print(result)
except Exception as ex:
print(ex)

UPDATE

import pymysql

# 資料庫設定
db_settings = {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "資料庫管理員密碼",
"db": "test2022",
"charset": "utf8"
}
try:
# 建立Connection物件
conn = pymysql.connect(**db_settings)

# 建立Cursor物件
with conn.cursor() as cursor:
# 修改資料SQL語法
command = "UPDATE member SET m_name = %s WHERE m_no = %s"
# 執行指令
cursor.execute(command, ("cchaha", "10"))

#儲存變更
conn.commit()
except Exception as ex:
print(ex)

DELETE

import pymysql

# 資料庫設定
db_settings = {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "資料庫管理員密碼",
"db": "test2022",
"charset": "utf8"
}
try:
# 建立Connection物件
conn = pymysql.connect(**db_settings)

# 建立Cursor物件
with conn.cursor() as cursor:
# 刪除特定資料指令
command = "DELETE FROM member WHERE m_no = %s"
# 執行指令
cursor.execute(command, ("10",))
#儲存變更
conn.commit()
except Exception as ex:
print(ex)
avatar-img
吳佳鑫的沙龍
41會員
136內容數
獨立遊戲開發紀錄
留言
avatar-img
留言分享你的想法!
吳佳鑫的沙龍 的其他內容
🗓 Steam 上市日期:2025/4/28 | 🖤 加入願望清單:https://store.steampowered.com/app/3634400/Elara/ 「他們說我太敏感,說我不合群。 但我只是——不想學會沉默。」 —Elara Quinn 你是否曾經在工作時,感
在靈異頻發的醫院夜班,小護士林筱筱意外發現她的冷酷上司——蘇醫生,竟然早已死亡五年,從此她與這位神秘的「鬼醫生」攜手處理靈異事件,並在驚魂不斷的夜班中逐漸建立起一段超越生死的羈絆。
這是一篇懸疑驚悚小說,描述女主角江語珊調查一起離奇命案的故事。死者林曦是一位心理學教授,死於自家公寓,現場佈滿鏡子,死者嘴角帶著詭異的微笑。語珊在調查過程中發現,此案與一種名為「鏡像侵蝕」的心理現象有關,並逐漸被捲入其中。
🗓 Steam 上市日期:2025/4/28 | 🖤 加入願望清單:https://store.steampowered.com/app/3634400/Elara/ 「他們說我太敏感,說我不合群。 但我只是——不想學會沉默。」 —Elara Quinn 你是否曾經在工作時,感
在靈異頻發的醫院夜班,小護士林筱筱意外發現她的冷酷上司——蘇醫生,竟然早已死亡五年,從此她與這位神秘的「鬼醫生」攜手處理靈異事件,並在驚魂不斷的夜班中逐漸建立起一段超越生死的羈絆。
這是一篇懸疑驚悚小說,描述女主角江語珊調查一起離奇命案的故事。死者林曦是一位心理學教授,死於自家公寓,現場佈滿鏡子,死者嘴角帶著詭異的微笑。語珊在調查過程中發現,此案與一種名為「鏡像侵蝕」的心理現象有關,並逐漸被捲入其中。