更新Mysql数据库的数据

学习任务

 用sql语句修改第一条记录name为xiaohua

 


学习目标

知识目标

1.      熟悉sql的更新数据语句

2.      熟悉对MySql数据库的更新操作

能力目标

1.能够编写sql更新语句

2.学会对MySql数据库的修改操作

 

 

更新语句

update 【表名】set【修改的值】 where 【查询条件】


 

 

 

示例

import MySQLdb             ||导入MySql模块
conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )                ||连接数据库
 


 

 

 

 

 

。。。

#用于创建数据库的连接
cur = conn.cursor()
#编写查询语句,返回查询的数据个数
#编写查询语句
sql1 = "sql1 = "update user set pwd='789' where name = 'lihua'";
";
#通过游标cur操作execute()方法写入纯sql语句来对数据进行操作
cur.execute(sql1);
 


 

 

 

 

 

 

 

#关闭数据库的连接
cur.close()
conn.commit()
conn.close()



 

 

 

 

 

 

 

 

 

 

 

 

 
 
 
 
任务实施

#!/user/bin/env python
# _*_ coding:utf-8 _*_
import MySQLdb
conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
#用于创建数据库的连接
cur = conn.cursor()
#编写查询语句
sql1 = "update student set age='15' where name ='xiaoye'";
#通过游标cur操作execute()方法写入纯
cur.execute(sql1);

cur.close()
conn.commit()
conn.close()


 
 
知识点总结
1. 使用sql语句更新数据
 
 
问题
1.  sql更新数据语句关键字

2.      用sql语句修改第一条记录name为xiaohua

 

3.      3. 用sql语句修改第二条记录name为lisan

 
 
答案
1. 
update 【表名】set【修改的值】 where 【查询条件】

 

2. 

#!/user/bin/env python
# _*_ coding:utf-8 _*_
import MySQLdb
conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
#用于创建数据库的连接
cur = conn.cursor()
#编写查询语句
sql1 = "update student set age='15' where name ='xiaoye'";
#通过游标cur操作execute()方法写入纯
cur.execute(sql1);

cur.close()
conn.commit()
conn.close()

 
修改后
 
 
 

3. 

#!/user/bin/env python
# _*_ coding:utf-8 _*_
import MySQLdb
conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
#用于创建数据库的连接
cur = conn.cursor()
#编写查询语句
sql1 = "update student set name='lisan' where age='12'";
#通过游标cur操作execute()方法写入纯
cur.execute(sql1);

cur.close()
conn.commit()
conn.close()


 
 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞飞翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值