Python MySQL更新操作
1、更新操作
- UPDATE-SET 語句用於更新表中的任何列。以下SQL查詢用於更新列。
> update Employee set name = 'alex' where id = 110
- 實例
import mysql.connector
myconn = mysql.connector.connect(host="192.168.126.20", user="root", passwd="mysql", database="PythonDB")
cur = myconn.cursor()
cur.execute("update Employee set name = 'alex' where id = 110")
myconn.commit() # commit() 使用該方法提交更新信息,注意是使用連接對象提交更新數據,查看數據使用的是游標對象
- 服務器查看結果輸出
2、刪除操作
-
DELETE FROM 語句用於從表中刪除特定記錄。在這里,我們必須使用WHERE子句強加條件,否則將刪除表中的所有記錄
-
以下SQL查詢用於從表中刪除id為110的員工詳細信息
> delete from Employee where id = 110
- 案例
import mysql.connector
myconn = mysql.connector.connect(host="192.168.126.20", user="root", passwd="mysql", database="PythonDB")
cur = myconn.cursor()
cur.execute("delete from Employee where id = 110")
myconn.commit()
- 服務器端查詢結果輸出