学习任务
使用sql语句删除姓名为jerry的人
左边图
右边图
学习目标
知识目标
1. 熟悉sql的删除记录语句
2. 熟悉对MySql数据库的删除操作
3.
能力目标
1. 能够对数据库进行删除操作
删除语句
deletefrom 【表名】where 【列名】 like '【检索的条件】
1.连接数据库
Conn 用于创建数据的连接
Cur 通过获取到的数据库连接conn下的cursor()方法来创建游标
2.
执行删除操作
第一句 编写sql语句
第二句 执行sql语句【通过游标cur 操作execute()方法可以写入纯sql语句。对数据进行操作。】
2. 关闭数据库连接
3.
示例
使用sql语句删除符合查询调价的数据
任务实施
录屏
知识点总结
1. 使用sql语句删除数据
2. 连接MySql数据库
问题
1. 删除表中名字为admin的记录
2. 同时删除表中名字为lihua和liliang的记录
3. 对数据库删除操作的sql语句格式是什么
答案
1.
#!/user/bin/env python # _*_ coding:utf-8 _*_ #coding=utf-8 import MySQLdb conn= MySQLdb.connect( host='localhost', port = 3306, user='root', passwd='123456', db ='test', ) cur = conn.cursor() sql ="delete from user where name='admin'"; cur.execute(sql); cur.close() conn.commit() conn.close()
2.
#!/user/bin/env python
# _*_ coding:utf-8 _*_
#coding=utf-8
import MySQLdb
conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='123456',
db ='test',
)
cur = conn.cursor()
sql1 = "delete from userwhere name like 'li*'";
cur.execute(sql1);
cur.close()
conn.commit()
conn.close()
3.delete from 【表名】where 【列名】 like '【检索的条件】'