python在数据库的基本操作(修改数据)

版权声明:版权归本人,仅供大家参考 https://blog.csdn.net/Dream____Fly/article/details/86655880

修改数据:

import pymysql

config = {
          'host':'localhost',
          'port':3306,
          'user':'root',
          'passwd':'root',
          'db':'logon',
          'charset':'utf8'
          }
# 打开数据库连接
conn = pymysql.connect(**config)

try:
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = conn.cursor()

    # SQL 更新语句
    sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 " \
          "WHERE SEX = '%c'" % ('M')

    # 执行SQL语句
    cursor.execute(sql)

    # 确认修改
    conn.commit()

    # 关闭游标
    cursor.close()

    # 关闭链接
    conn.close()
    print("修改成功")
except:
    print("修改失败")

猜你喜欢

转载自blog.csdn.net/Dream____Fly/article/details/86655880