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()
- 服务器端查询结果输出