# 关键字exists(了解)
只返回布尔值 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可视化界面操作数据
  • 数据库查询题目讲解(多表操作)
  • python如何操作MySQL(pymysql模块)
  • sql注入问题
  • pymysql模块增删改查数据操作

今日内容详细

Navicat软件

"""
一开始学习python的时候 下载python解释器然后直接在终端书写
pycharm能够更加方便快捷的帮助你书写python代码
excel word pdf 我们在终端操作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=''
"""
日常生活中很多软件在注册的时候都不能含有特殊符号
因为怕你构造出特定的语句入侵数据库 不安全 # 敏感的数据不要自己做拼接 交给execute帮你拼接即可
# 结合数据库完成一个用户的登录功能?
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"
# 不要手动拼接数据 先用%s占位 之后将需要拼接的数据直接交给execute方法即可
print(sql)
rows = cursor.execute(sql,(username,password)) # 自动识别sql里面的%s用后面元组里面的数据替换
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(res) # execute返回的是你当前sql语句所影响的行数 改返回值一般不用
# 获取命令执行的查询结果
# print(cursor.fetchone()) # 只拿一条
# print(cursor.fetchall()) # 拿所有
# print(cursor.fetchmany(2)) # 可以指定拿几条
print(cursor.fetchone())
print(cursor.fetchone()) # 读取数据类似于文件光标的移动
# cursor.scroll(1,'relative') # 相对于光标所在的位置继续往后移动1位
cursor.scroll(1,'absolute') # 相对于数据的开头往后继续移动1位
print(cursor.fetchall())

pymysql补充

# 1.针对增删改 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.execute(sql,('jackson',123))
rows = cursor.executemany(sql,[('xxx',123),('ooo',123),('yyy',123)])
print(rows)
# conn.commit() # 确认
# 修改
# sql = 'update user set name="jasonNB" where id=1'
# rows = cursor.execute(sql)
# print(rows)
# conn.commit() # 确认
# 删除
sql = 'delete from user where id=7'
rows = cursor.execute(sql)
print(rows)
conn.commit() # 确认
# 查
# sql = 'select * from user'
# cursor.execute(sql)
# print(cursor.fetchall()) """
增删改查中
删改增它们的操作设计到数据的修改
需要二次确认
""" # 还可以一次性插入N多条数据
rows = cursor.executemany(sql,[('xxx',123),('ooo',123)])

最新文章

  1. xv6的课本翻译之——附录B 系统启动器
  2. 《DSP using MATLAB》示例Example6.1
  3. 大熊君学习html5系列之------XHR2(XMLHttpRequest Level 2)
  4. linux网络编程-(socket套接字编程UDP传输)
  5. C# 文件大小
  6. 关于form 上传文件时的小问题
  7. 字符流缓冲区的使用之BufferedWriter和BufferedReader
  8. git应用套路
  9. MySQL数据库实用技巧
  10. 轮播图插件swiper 的使用
  11. AFNetWorking上传JSON串
  12. 白鹭引擎 - 显示对象与 HelloWord ( 绘制了一个红蓝相间的 2 x 2 格子 )
  13. Java 动态代理 两种实现方法
  14. [leetcode]Palindrome Partitioning II @ Python
  15. 数据库链接 mybatis spring data jpa 两种方式
  16. IBM X3650M4简单排错方法
  17. 万网知您所需,“域”众不同--.link/.love/.help等一大波新顶级域来袭!
  18. Nginx集群配置与redis的session共享策略
  19. POJ - 1080 枚举 / DP
  20. org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.oskyhang.gbd.service.UserService] found for dependency: expected at least 1 bean which qualifies as aut

热门文章

  1. HBase-2.2.3源码编译-Windows版
  2. 怎么入门python?不懂你别瞎尝试,看看大佬怎么说
  3. 别人用钱,而我用python爬虫爬取了一年的4K高清壁纸
  4. O - Navigation System CodeForces - 1321D
  5. F - Distinct Numbers
  6. 深入理解Java线程状态转移
  7. selenium获取多窗口句柄并一切换至原窗口句柄(三个窗口)
  8. 5. git 过滤,让某文件夹里无法提交新添加的文件
  9. MySQL服务端恶意读取客户端文件漏洞 (DDCTF2019和国赛均涉及到这个漏洞)
  10. [JS] 自己弄得个倒计时