Navicat 软件的使用以及pymysql
Navicat 软件的使用以及pymysql
一、navicate的安装及使用
下载
直接百度搜索navicate ,如下图

连接数据库


新建数据库以及新建表
选中然后鼠标右键


- 建模


- 利用navicate去查询练习
-- 查询所有的课程的名称以及对应的任课老师的姓名
-- SELECT
-- course.cname,
-- teacher.tname
-- FROM
-- course
-- INNER JOIN teacher ON course.teacher_id = teacher.tid;

-- 查询平均成绩大于80分的同学的姓名和平均成绩
SELECT
student.sname,
t1.av
FROM
student
INNER JOIN (
SELECT
score.student_id,
avg( score.num ) AS av
FROM
score
GROUP BY
score.student_id
HAVING
avg( score.num ) > 80
) AS t1 ON student.sid = t1.student_id;

-- 查询没有同时报李平老师课的学生姓名
-- 1、查李平老师教授的课程id
-- 2、去score表中查询报了李平老师课程的学生id
-- 3、再去学生表中查学生的姓名
SELECT
*
FROM
student
WHERE
student.sid NOT IN (
SELECT DISTINCT
score.student_id
FROM
score
WHERE
score.course_id IN ( SELECT course.cid FROM course INNER JOIN teacher ON course.teacher_id = teacher.tid WHERE teacher.tname = '李平老师' )
);

查询没有同时选修物理课程和体育课题的学生姓名(只能在两者间选一门)
-- 1、先查询物理以及体育的id号
#2、先拿到所有报了物理、体育的学生的id
SELECT
student.sname
FROM
student
WHERE
student.sid IN (
SELECT
score.student_id
FROM
score
WHERE
score.course_id IN ( SELECT course.cid FROM course WHERE course.cname IN ( '物理', '体育' ) )
GROUP BY
score.student_id
HAVING
COUNT( score.course_id ) = 1
);

-- 查询挂科超过两门(包括两门)的学生姓名和班级
# 1、先拿所有分数小于60的
SELECT
student.sname,
class.caption
FROM
student
INNER JOIN class ON student.class_id = class.cid
WHERE
student.sid IN ( SELECT score.student_id FROM score WHERE num < 60 GROUP BY score.student_id HAVING count( score.course_id ) >= 2 );

二、pymysql
- 初识
import pymysql
coon = pymysql.connect(
user = 'root',
password = '123456',
host = '127.0.0.1',
port = 3306,
charset = 'utf8',
database = 'day36_1'
)
cursor = coon.cursor(cursor=pymysql.cursors.DictCursor) # 产生了一个游标对象
# cursor=pymysql.cursors.DictCursor 将查询出来的结果制作成字典的形式返回
sql = 'select * from student'
res = cursor.execute(sql) # 执行sql语句
# print(res) # execute返回的是当前SQL受影响的行数
# ret = cursor.fetchone() # 只获取查询结果中的一条数据
# ret = cursor.fetchall() # 获取查询结果中的所有数据
# ret = cursor.fetmany() # 指定获取几条数据 如果数字超了也不会报错
# print(ret)
print(cursor.fetchone())
print(cursor.fetchone())
# 相对移动
cursor.scroll(2, 'relative') # 基于指针所在的位置 往后偏移
# 绝对移动
# cursor.scroll(3, 'absolute') # 基于起始位置 往后偏移
print(cursor.fetchall())
相对移动

绝对移动

- sql注入问题
import pymysql
coon = pymysql.connect(
user = 'root',
password = '123456',
db = 'day36_1',
host = '127.0.0.1',
port = 3306,
charset = 'utf8'
)
cursor = coon.cursor(cursor=pymysql.cursors.DictCursor)
#获取用户输入的用户名密码,然后去数据库中校验
username = input('username>>>:').strip()
password = input('password>>>:').strip()
sql = "select * from emp where name = '%s' and password = '%s'" %(username, password)
cursor.execute(sql)
res = cursor.fetchall()
if res:
print(res)
else:
print('username or password error!')
# 一、只知道用户名
# username>>>:yafeng ' -- daflakjflal
# password>>>:
# [{'id': 1, 'name': 'yafeng', 'password': '123'}]
# 二、用户名密码都不知道
# username>>>:xxx' or 1=1 -- dalfjakdaj
# password>>>:
# [{'id': 1, 'name': 'yafeng', 'password': '123'}]
'''
sql 注入问题
利用特殊符号和注释语法,巧妙的绕过真正的sql校验
解决方案
关键性的数据,不要自己手动去拼接, 而是交由execute帮你去做拼接
'''


- 解决注入问题
import pymysql
coon = pymysql.connect(
user = 'root',
password = '123456',
db = 'day36_1',
host = '127.0.0.1',
port = 3306,
charset = 'utf8'
)
cursor = coon.cursor(cursor=pymysql.cursors.DictCursor)
#获取用户输入的用户名密码,然后去数据库中校验
username = input('username>>>:').strip()
password = input('password>>>:').strip()
sql = "select * from emp where name = %s and password = %s"
print(sql)
cursor.execute(sql, (username, password))
res = cursor.fetchall()
if res:
print(res)
else:
print('username or password error!')



- 数据的增删改查
import pymysql
coon = pymysql.connect(
user = 'root',
password = '123456',
db = 'day36_1',
host = '127.0.0.1',
port = 3306,
charset = 'utf8',
autocommit = True # 自动提交确认
)
cursor = coon.cursor(cursor=pymysql.cursors.DictCursor)
#
# # 获取用户输入的用户名和密码, 然后去数据库中校验
# username = input('username>>>:').strip()
# password = input('password>>>:').strip()
#
# sql = "select * from userinfo where name=%s and password=%s"
# print(sql)
'''
针对增删改的操作 执行重要程度偏高
如果真想要操作 必须有进一步确认操作(commit)
'''
# 增
# sql = "insert into emp(name,password) values('jason',456)"
# 改
# sql = "update emp set name='jason_nb' where id = 2"
# 删
sql = "delete from emp where id = 1"
res = cursor.execute(sql)
print(res)
Navicat 软件的使用以及pymysql的更多相关文章
- Navicat,SQL注入,pymysql模块
# 关键字exists(了解) 只返回布尔值 True False 返回True的时候外层查询语句执行 返回False的时候外层查询语句不再执行 select * from emp where exi ...
- MySQL聚合函数、控制流程函数(含navicat软件的介绍)
MySQL聚合函数.控制流程函数(含navicat软件的介绍) 一.navicat的引入:(第三方可视化的客户端,方便MySQL数据库的管理和维护) NavicatTM是一套快速.可靠并价格相宜的数据 ...
- navicat软件设置连接mysql数据库
navicat软件设置连接mysql数据库 适用范围及演示使用工具 适用范围:mysql全部系列(含Linux和Windows系统下的mysql) 演示使用工具:Navicat 8.0 MySQL 演 ...
- day43——多表查询、Navicat工具的使用、pymysql模块
day43 多表查询 笛卡尔积--不经常用 将两表所有的数据一一对应,生成一张大表 select * from dep,emp; # 两个表拼一起 select * from dep,emp wher ...
- navicat软件、 python操作MySQL
查询关键字之having过滤 having与where的功能是一模一样的 都是对数据进行筛选 where用在分组之前的筛选 havng用在分组之后的筛选 为了更好的区分 所以将where说成筛选 ha ...
- Navicat软件中mysql中int、bigint、smallint和tinyint的区别、布尔类型存储以及乱码问题的解决
很长时间不写博客了,最近一直在忙这学校的比赛都忘记更新博客了.新的任务又要开始了,我们要准备<2017年中国大学生计算机设计大赛软件服务外包竞赛>.这次不能再想像之前那样有PC端的功能作为 ...
- MySQL数据库学习笔记(四)----MySQL聚合函数、控制流程函数(含navicat软件的介绍)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- Navicat软件安装
Navicat_10.1.7永久注册码 NAVH-WK6A-DMVK-DKW3
- python 全栈开发,Day63(子查询,MySQl创建用户和授权,可视化工具Navicat的使用,pymysql模块的使用)
昨日内容回顾 外键的变种三种关系: 多对一: 左表的多 对右表一 成立 左边的一 对右表多 不成立 foreign key(从表的id) refreences 主表的(id) 多对多 建立第三张表(f ...
随机推荐
- MyBatis—resultMap 的关联方式实现多表查询(多 对一)
mapper 层 a)在 StudentMapper.xml 中定义多表连接查询 SQL 语句, 一次性查到需要的所有数据, 包括对应班级的信息. b)通过<resultMap>定义映射关 ...
- 当placeholder的字体大小跟input大小不一致时,实现placeholder垂直居中
如图:搜索和图标不是垂直居中着实难受 最终通过如下代码实现: input::-webkit-input-placeholder { transform: translate(0, 2px); }
- Vue如何实现数据响应的
参考博客:https://medium.com/vue-mastery/the-best-explanation-of-javascript-reactivity-fea6112dd80d 翻译博客: ...
- 解决:Sass Loader has been initialised using an options object that does not ma tch the API schema.
今天是犯傻的一天,第一回用sass遇到了bug: 结果就是:<style lang = 'scss'>.写成了<style lang = 'sass'> (脑子要清醒一点.太笨 ...
- eps-07s,编译及其烧写
项目导入 清理并编译 会出现两个bin文件,然后烧写 修改红框中的东西,然后返回操作界面,进行一键烧写 硬件接线图 设备调试
- 函数式编程 -> Lambda
一.函数式编程 函数式编程,同面向对象编程.指令式编程一样,是一种软件编程范式,在多种编程语言中都有应用.百科词条中有很学术化的解释,但理解起来并不容易.不过,我们可以借助于数学中函数的概念,来理解函 ...
- 规范git commit提交记录和版本发布记录
在开发过程中我们一般都会用到git管理代码,在git commit提交代码时我们一般对git commit message随便写点简单的描述,可是随着项目参与人数的增多,发现提交的commit记录越来 ...
- 《CSAPP》实验二:二进制炸弹
二进制炸弹是第三章<程序的机器级表示>的配套实验,这章主要介绍了x64汇编,包括:操作数的表示方式,数据传送指令,算术和逻辑指令,控制流跳转指令,过程(procedure)的实现与运行时栈 ...
- 【并发编程】Java并发编程传送门
本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. [并发编程系列博客传送门](https://www.cnblogs.com/54 ...
- Thread.Sleep线程休眠-小白向
try { Thread.sleep(); } catch (InterruptedException e) { } 首先这段代码的作用是使当前进程沉睡2S,展现给用户的结果就是画面维持两秒,有个“正 ...