zoukankan      html  css  js  c++  java
  • pymysql 连接的基本使用,注入问题。

    mysql 基本使用:

    mysql连接数据库

    import pymysql
    
    conn = pymysql.connect(
        user = 'root',
        password = '1999',
        host = '127.0.0.1',
        port = 3306,
        charset = 'utf8',
        database = 'dep2'  #库名
    )
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  # 产生一个游标对象
    # cursor=pymysql.cursors.DictCursor 将查询出来的结果制作成字典的形式返回
    sql = "select * from dep2"     # dep2 ---> 表名
    res = cursor.execute(sql)  # 执行sql语句
    print(res)  #execute返回的时候当前sql所影响的行数
    
    # 结果: dep2因为表中就只有两行数据 。execute只打印他的行式
    2
    
    ret = cursor.fetchone()  # 只获取查询结果中的一条数据
    print(ret)
    #结果:
    {'id': 2, 'dep_name': '教学部', 'dep_desc': '造程序员部门!', 'dep_company': '老男孩教育有限公司'}
    
    ret = cursor.fetchall()  # 获取查询结果的所有数据
    print(ret)
    #结果:
    [{'id': 2, 'dep_name': '教学部', 'dep_desc': '造程序员部门!', 'dep_company': '老男孩教育有限公司'}, {'id': 3, 'dep_name': '网络部', 'dep_desc': '技术部门', 'dep_company': '嘉兴互联网有限公司'}]
    
    ret = cursor.fetchmany(4)  # 指定获取几条数据  如果数字超了也不会报错
    print(ret)
    #结果:
    [{'id': 2, 'dep_name': '教学部', 'dep_desc': '造程序员部门!', 'dep_company': '老男孩教育有限公司'}, {'id': 3, 'dep_name': '网络部', 'dep_desc': '技术部门', 'dep_company': '嘉兴互联网有限公司'}]
    

    重要关键词:

    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  # 产生一个游标对象
    # cursor=pymysql.cursors.DictCursor 将查询出来的结果制作成字典的形式返回
    res = cursor.execute(sql)  # 执行sql语句 
    # execute返回的时候当前sql所影响的行数
    # ret = cursor.fetchone()  # fetchone:只获取查询结果中的一条数据
    # ret = cursor.fetchall()  # fetchall:获取查询结果的所有数据
    # ret = cursor.fetchmany(4)  # fetchmany指定获取几条数据  如果数字超了也不会报错
    
    # fetchone:只获取查询结果中的一条数据,如果超出范围则返回None
    print(cursor.fetchone())   # {'id': 2, 'dep_name': '教学部', 'dep_desc': '造程序员部门!', 'dep_company': '老男孩教育有限公司'}
    print(cursor.fetchone())   # {'id': 2, 'dep_name': '教学部', 'dep_desc': '造程序员部门!', 'dep_company': '老男孩教育有限公司'}
    print(cursor.fetchone())  #:None
    
    conn.commit()  #运行时必须先提交conn.commit()
    cursor.scroll(1,'relative')  # 基于指针所在的位置 往后偏移 
    # print(cursor.scroll())
    [{'id': 2, 'dep_name': '网络部', 'dep_desc': '技术部门', 'dep_company': '嘉兴互联网有限公司'}]
    
    conn.commit()  #运行时必须先提交conn.commit()
    cursor.scroll(1,'absolute')  # 基于起始位置 往后偏移  就相当于间隔的意思。
    print(cursor.fetchall())
    
    # 结果:因为表中只有两行数据 我传入的数字是1 间隔1行打印 。
    [{'id': 3, 'dep_name': '网络部', 'dep_desc': '技术部门', 'dep_company': '嘉兴互联网有限公司'}]
    

    注入问题:

    sql注入问题
        利用特殊符号和注释语法 巧妙的绕过真正的sql校验
    
    关键性的数据 不要自己手动去拼接 而是交由execute帮你去做拼接
    
    import pymysql
    
    
    conn = pymysql.connect(
        user = 'root',
        passwd = '123456',
        db = 'day36',
        host = '127.0.0.1',
        port = 3306,
        charset = 'utf8'
    )
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    # 获取用户输入的用户名和密码 然后取数据库中校验
    username = input('username>>>:').strip()
    password = input('password>>>:').strip()
    # sql = "select * from userinfo where name='%s' and password= '%s'"%(username,password)
    sql = "select * from userinfo 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!')
    
    
    sql = "select * from userinfo where name='%s' and password= '%s'"%(username,password)
    
    print(sql)   #拼接的方法,不需要密码也可以登录
    
    sql = "select * from userinfo where name=%s and password= %s"
    print(sql)  # 
    

    数据的增、删、改、查。

    import pymysql
    
    
    conn = pymysql.connect(
        user = 'root',
        passwd = '1999',
        db = 'dep2',
        host = '127.0.0.1',
        port = 3306,
        charset = 'utf8',
        autocommit = True  # 自动提交确认
    )
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    # # 获取用户输入的用户名和密码 然后取数据库中校验
    # username = input('username>>>:').strip()
    # password = input('password>>>:').strip()
    # # sql = "select * from userinfo where name='%s' and password= '%s'"%(username,password)
    # sql = "select * from userinfo where name=%s and password= %s"
    # print(sql)
    
    # 删除
    # sql = "delete from userinfo where id= 1"
    res = cursor.execute(sql)
    # conn.commit()  # 确认当前操作  真正的同步到数据库
    print(res)
    
    """
    针对增 删 改操作 执行重要程度偏高 
    你如果真想操作  必须有一步确认操作(commit)
    """
    
    

    sql = "insert into dep2(name,password,dep_id) values('jason',789,1)"
    

    sql = "update userinfo set name='egondsb' where id = 6"
    

    删除

    # sql = "delete from userinfo where id= 1"
    res = cursor.execute(sql)
    
    # conn.commit()  # 确认当前操作  真正的同步到数据库
    
  • 相关阅读:
    Oracle 分析函数(Analytic Functions) 说明
    Build Your Own Oracle RAC 10g Release 2 Cluster on Linux and FireWire
    Build Your Own Oracle RAC 10g Release 2 Cluster on Linux and FireWire
    ORACLE SEQUENCE 介绍
    RAC Ocfs2文件系统常见问题解决方法
    linux 下修改日期和时间
    寒假刷题之7——波纹
    iOS 游戏 Oh my fish! 切割痕迹实现
    ACM常识
    寒假刷题之6——迷宫
  • 原文地址:https://www.cnblogs.com/WQ577098649/p/12051243.html
Copyright © 2011-2022 走看看