PyMySQL的更新 UPDATE
程序员文章站
2022-06-01 13:29:41
...
import pymysql
class Oper_Sql:
# 更新语句
update_sql = 'update self_user set name="other" where id="1001" '
def main():
try:
conn = pymysql.connect(host='localhost',
user='root',
password='root',
db='scott',
charset='utf8'
)
with conn.cursor() as cursor:
cursor.execute(Oper_Sql.update_sql)
# 必须有 conn.commit() 否则数据库不能得到执行结果
conn.commit()
print('更新成功!')
except Exception as err:
print(err)
finally:
conn.close()
if __name__ == '__main__':
main()
- 原始表数据
-- 要修改的原始数据
mysql> select * from self_user where id='1001';
+------+-------+------+------+------------+
| id | name | sex | age | birthday |
+------+-------+------+------+------------+
| 1001 | aidou | male | 32 | 2020-02-23 |
+------+-------+------+------+------------+
1 row in set (0.00 sec)
mysql> select * from self_user;
+------+-------+--------+------+------------+
| id | name | sex | age | birthday |
+------+-------+--------+------+------------+
| 1001 | aidou | male | 32 | 2020-02-23 |
| 1002 | moli | female | 12 | 2020-03-23 |
| 1003 | ali | male | 32 | 2020-03-04 |
| 1004 | boby | male | 19 | 1996-05-04 |
| 0001 | nihao | male | 43 | 2020-03-21 |
+------+-------+--------+------+------------+
5 rows in set (0.00 sec)
- 执行更新后的数据(
更新 ID = '1001'
name = other
) 结果提示: 更新成功!
mysql> select * from self_user;
+------+-------+--------+------+------------+
| id | name | sex | age | birthday |
+------+-------+--------+------+------------+
| 1001 | other | male | 32 | 2020-02-23 |
| 1002 | moli | female | 12 | 2020-03-23 |
| 1003 | ali | male | 32 | 2020-03-04 |
| 1004 | boby | male | 19 | 1996-05-04 |
| 0001 | nihao | male | 43 | 2020-03-21 |
+------+-------+--------+------+------------+
5 rows in set (0.00 sec)
-- 抽离出执行成功的 SQL
mysql> select * from self_user where id=1001;
+------+-------+------+------+------------+
| id | name | sex | age | birthday |
+------+-------+------+------+------------+
| 1001 | other | male | 32 | 2020-02-23 |
+------+-------+------+------+------------+
上一篇: 面粉坏了还能吃吗?不能吃了
下一篇: Myeclipse8.5下载 汉化【转】
推荐阅读
-
win10 Build 9926更新出现错误代码0x80240020的解决方法
-
网站内容更新难 互联网那些免费的资源为啥不用
-
Chrome更新失败出现错误代码:0x00000000的解决方法
-
用驱动精灵怎么更新显卡驱动以及更新显卡驱动出现蓝屏的解决方法
-
Angular2中监听数据更新的方法
-
Win10 Creators Update创作者更新定型:第三次重磅大补
-
安装程序不能验证Update.inf文件的完整性,请确定加密服务正在此计算机上运行
-
VS2015在升级到Update2之后运行Cordova项目异常的解决方案
-
win7更新显卡驱动后黑屏怎么办?更新显卡驱动后黑屏的解决方法
-
SQL Server 触发器 表的特定字段更新时,触发Update触发器