学习任务
用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()