• Navicat,SQL注入,pymysql模块


    # 关键字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模块增删改查数据操作

    今日内容详细

    """
    一开始学习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)])
    
  • 相关阅读:
    高速传输线PCB设计
    带状线和微带线
    资源分配
    异步时钟切换电路
    Mathcad操作tips:2D绘图
    Mathcad操作tips:函数、符号计算
    慢性胃炎注意事项
    Arduino I2C + 三轴加速度计ADXL345
    Arduino SPI + SPI Flash芯片W25Q80BV
    Arduino I2C + 三轴加速度计LIS3DH
  • 原文地址:https://www.cnblogs.com/pythonwl/p/12845066.html
Copyright © 2020-2023  润新知