数据库操作:Python如何更新MySQL?

UPDATE-SET语句用于更新表中的任何列。以下SQL查询用于更新列。

>  update Employee set name = 'alex' where id = 110

考虑以下示例。

例子

import mysql.connector

#Create the connection object 
myconn = mysql.connector.connect(host = "localhost", user = "root", passwd = "google", database = "PythonDB")

#creating the cursor object
cur = myconn.cursor()

try:
    #updating the name of the employee whose id is 110
    cur.execute("update Employee set name = 'alex' where id = 110")
    myconn.commit()
except:
    
    myconn.rollback()

myconn.close()
更新操作

删除操作

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

以下SQL查询用于从表中删除ID为110的员工明细。

>  delete from Employee where id = 110

考虑以下示例。

例子

import mysql.connector

#Create the connection object 
myconn = mysql.connector.connect(host = "localhost", user = "root", passwd = "google", database = "PythonDB")

#creating the cursor object
cur = myconn.cursor()

try:
    #Deleting the employee details whose id is 110
    cur.execute("delete from Employee where id = 110")
    myconn.commit()
except:
    
    myconn.rollback()

myconn.close()

来源:

https://www.srcmini02.com/31276.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_35540.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?