1小時學(xué)會 Python操作Mysql數(shù)據(jù)庫之pymysql模塊技術(shù):https://www.bilibili.com/video/BV1Dz4y1j7Jr
執(zhí)行update操作,雷同前面的insert操作
from pymysql import Connection
con = None
try:
# 創(chuàng)建數(shù)據(jù)庫連接
con = Connection(
host="localhost", # 主機名
port=3306, # 端口
user="root", # 賬戶
password="123456", # 密碼
database="db_python", # 指定操作的數(shù)據(jù)庫
autocommit=True # 設(shè)置自動提交
)
# 獲取游標對象
cursor = con.cursor()
# 使用游標對象,執(zhí)行sql語句
cursor.execute("UPDATE t_student SET age=19 WHERE id=5")
# 確認提交
# con.commit()
except Exception as e:
print("異常:", e)
finally:
if con:
# 關(guān)閉連接
con.close()