Python MySQL更新操作


Python MySQL更新操作

1、更新操作

  1. UPDATE-SET 语句用于更新表中的任何列。以下SQL查询用于更新列。
>  update Employee set name = 'alex' where id = 110
  1. 实例
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、删除操作

  1. DELETE FROM 语句用于从表中删除特定记录。在这里,我们必须使用WHERE子句强加条件,否则将删除表中的所有记录

  2. 以下SQL查询用于从表中删除id为110的员工详细信息

>  delete from Employee where id = 110
  1. 案例
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()  
  • 服务器端查询结果输出


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM