python day42 数据库——pymysql模块 增删改和sql注入 数据备份和事务

一、内容回顾

  • b+ 树 (b是baance 平衡)

    • 为了保证每个数据查找经历的io次数相等

    • 只在叶子节点存储数据

      • 为了降低树的高度

    • 叶子节点之间加入了双向的连接

      • 为了查找范围的时候比较快

  • 聚集索引(聚簇)

    • 全表数据存储在叶子节点上,Innodb存储引擎中的主键

  • 非聚集索引(非聚簇) 辅助索引

    • 叶子节点不存放具体的整行数据,而是存储的是这一行主键的值

  • 索引的创建

    • create index 索引名 on 表名(字段名)

    • create index 索引名 on 表名(字段名1,字段名2)

  • 删除索引

    • drop index 索引名 on 表名

  • 正确的使用mysql数据库

    • 从库的角度的

      • 搭建集群

      • 读写分离

      • 分库

    • 从表的角度

      • 合理安排表关系

      • 把固定长度的字段放前面

      • 尽量使用char 而不是varchar

    • 从操作数据的角度

      • 尽量在where字段约束数值到一个比较小的范围

      • 尽量使用链表查询代替子查询

      • 删除数据和修改数据的时候条件尽量使用主键

      • 合理的创建和使用索引

        • 1.创建索引

          • 选择区分度比较大的列

          • 尽量选择长度比较短的列

            • 不要创建不必要的索引

          • 使用索引

            • 查询的字段不是索引字段

            • where条件字段索引字段在条件中要排在第一个

            • 在条件中使用范围,范围越大速度越慢,范围越小速度越快

            • like 'a%' 可以命中索引,

            • 条件列不能参与计算,也不能使用函数

            • 在and 条件相连,有一列有索引都会命中

            • or条件相连,所有列都有索引才能命中

            • 条件中的数据类型和实际字段必须一致

            • select 字段中应该包含order by中的字段

            • 联合索引:遵循最左前缀原则,且从出现范围开始,索引失效

    • 执行计划: explain select 语句 能够查看sql语句有没有按照预期执行,可以查看索引的使用情况 type等级

    • 慢查询的优化:

      • 从sql的角度优化

        • 把每一句话单独执行,找到效率低的表,优化这句sql

        • 了解业务场景,适当创建一些索引,帮助查询

        • 尽量用连表代替子查询

        • 确认命中索引情况

      • 考虑修改表结构

        • 拆分表结构

        • 把固定的字段往前调整

      • 使用执行计划,观察sql的type通过以上调整是否提高

  • sql的慢日志

    • 在mysql的配置中开启并设置一下

 

 

二、今日内容

1、pymysql模块

  • 连接数据库

  • 获取游标

  • 执行sql() 增删改查

  • 如果涉及到修改,提交 commit()

  • 关闭游标

  • 关闭数据库

    import pymysql
    import pymysql
    #
    conn = pymysql.connect(host='127.0.0.1',
                          user='root',
                          password="123",
                          database='homework')
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)  # 查询返回字典
    cur = conn.cursor()  # cursor游标
    cur.execute('select * from student;')
    print(cur.rowcount)   # 获取查出多少行,便于使用fetchone取所有结果
    for i in range(cur.rowcount):
       ret = cur.fetchone()      # 获取一条结果
       print(ret)

    try:
       cur.execute('select * from student;')
       ret = cur.fetchone()      # 获取一条结果
       print(ret)
       ret2 = cur.fetchmany(10)  # 获取多条结果
       print(ret2)
       ret3 = cur.fetchall()     # 获取全部结果
       print(ret3)
    except pymysql.err.ProgrammingError as e:
       print(e)
    cur.close()
    conn.close()


    # 增加 删除 修改
    conn = pymysql.connect(host='127.0.0.1',
                          user='root',
                          password="123",
                          database='homework')
    cur = conn.cursor()  # cursor游标
    try:
       cur.execute('insert into student(gender,class_id,sname) values("男",3,"大壮")')
       # cur.execute('update student set gender = "女" where sid = 17')
       # cur.execute('delete from student where sid = 17')
       conn.commit()
    except Exception as e:
       print(e)
       conn.rollback()     # 可以试一下 myisam
    cur.close()
    conn.close()

    # 实际操作mysql的时候会遇到的一个问题

    # 结合数据库 和python 写一个登录
    user = input('username :')
    pwd = input('password :')
    conn = pymysql.connect(host='127.0.0.1',
                          user='root',
                          password="123",
                          database='day42')
    sql = 'select * from userinfo where cname = %s and password = %s'
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute(sql,(user,pwd))
    print(cur.fetchone())

    # sql注入
    select * from userinfo where user = "1869" or 1=1;-- " and password = "3714";

     

2、 sql注入

  • 传参数: 注意sql注入问题,传参数通过execute方法来传递

  • excute('select * from 表 where name = %s')

    user = input('username :')
    pwd = input('password :')
    conn = pymysql.connect(host='127.0.0.1',
                          user='root',
                          password="123",
                          database='day42')
    sql = 'select * from userinfo where cname = %s and password = %s'
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute(sql,(user,pwd))
    print(cur.fetchone())

     

3、开启事务

  • begin开启

  • commit提交

    conn = pymysql.connect(host='127.0.0.1',
                          user='root',
                          password="123",
                          database='homework')
    cur = conn.cursor()  # cursor游标
    try:
       cur.execute('insert into student(gender,class_id,sname) values("男",3,"大壮")')
       # cur.execute('update student set gender = "女" where sid = 17')
       # cur.execute('delete from student where sid = 17')
       conn.commit()
    except Exception as e:
       print(e)
       conn.rollback()     # 可以试一下 myisam
    cur.close()
    conn.close()

     

4、数据的备份和恢复

  • 表和数据的备份 备份数据 在cmd命令行直接执行 mysqldump -uroot -p123 -h127.0.0.1 homework > D:\python_22\day42\tmp.sql

    恢复数据 在mysql中执行命令 切换到一个要备份的数据库中 source D:\python_22\day42\tmp.sql

  • 备份库 备份 mysqldump -uroot -p123 --databases homework > D:\python_22\day42\tmp2.sql 恢复 source D:\python_22\day42\tmp2.sql

posted @ 2021-02-23 16:06  小鱼鱼与黄黄  阅读(41)  评论(0编辑  收藏  举报