只返回布尔值 True False
返回True 的时候外层查询语句执行
返回False 的时候外层查询语句不再执行
select * from emp where exists
(select id from dep where id >3 );
select * from emp where exists
(select id from dep where id >300 );
Navicat软件
"""
我们在终端操作MySQL 也没有自动提示也无法保存等等 不方便开发
Navicat内部封装了所有的操作数据库的命令
用户在使用它的时候只需要鼠标点点即可完成操作 无需书写sql语句
"""
pymysql模块
"""
支持python代码操作数据库MySQL
"""
pip3 install pymysql
sql注入
"""
利用一些语法的特性 书写一些特点的语句实现固定的语法
MySQL利用的是MySQL的注释语法
select * from user where name='jason' -- jhsadklsajdkla' and password=''
select * from user where name='xxx' or 1=1 -- sakjdkljakldjasl' and password=''
"""
日常生活中很多软件在注册的时候都不能含有特殊符号
因为怕你构造出特定的语句入侵数据库 不安全
import pymysql
conn = pymysql.connect(
host = '127.0.0.1' ,
port = 3306 ,
user = 'root' ,
password = '123456' ,
database = 'day48' ,
charset = 'utf8'
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
username = input ('>>>:' )
password = input ('>>>:' )
sql = "select * from user where name=%s and password=%s"
print (sql)
rows = cursor.execute(sql,(username,password))
if rows:
print ('登录成功' )
print (cursor.fetchall())
else :
print ('用户名密码错误' )
import pymysql
conn = pymysql.connect(
host = '127.0.0.1' ,
port = 3306 ,
user = 'root' ,
password = '123456' ,
database = 'day48' ,
charset = 'utf8'
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
"""
cursor=pymysql.cursors.DictCursor将查询结果以字典的形式返回
"""
sql = 'select * from teacher;'
res = cursor.execute(sql)
print (cursor.fetchone())
print (cursor.fetchone())
cursor.scroll(1 ,'absolute' )
print (cursor.fetchall())
pymysql补充
import pymysql
conn = pymysql.connect(
host = '127.0.0.1' ,
port = 3306 ,
user = 'root' ,
passwd = '123456' ,
db = 'day48' ,
charset = 'utf8' ,
autocommit = True
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
sql = 'insert into user(name,password) values(%s,%s)'
rows = cursor.executemany(sql,[('xxx' ,123 ),('ooo' ,123 ),('yyy' ,123 )])
print (rows)
sql = 'delete from user where id=7'
rows = cursor.execute(sql)
print (rows)
conn.commit()
"""
增删改查中
删改增它们的操作设计到数据的修改
需要二次确认
"""
rows = cursor.executemany(sql,[('xxx' ,123 ),('ooo' ,123 )])
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现