zoukankan      html  css  js  c++  java
  • python 操作 MySQL 即相关问题

    导入pymysql

    import pymysql
    
    # 创建connect()对象
    conn = pymysql.connect(
        host = '127.0.0.1',
        port = 3306,
        user = 'root',
        password = 'root',
        database = 'db1',
        charset = 'utf8'
    )
    # 产生一个游标对象 以字典的形式返回查询出来的数据,键是表的字段,值时字段对应的记录
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    # 编写sql语句,赋值给一个变量
    sql = 'select * from teacher'
    # 执行传入的sql语句
    res = cursor.execute(sql)
    # print(res)  # res 是执行语句返回的数据条数
    print(cursor.fetchone())  # fetchone只获取一条数据 {'tid': 1, 'tname': '张磊老师'}
    print(cursor.fetchone())  # 只获取一条数据 {'tid': 2, 'tname': '李平老师'} 获取的是第二条数据,因为游标在移动
    
    # 控制光标移动
    cursor.scroll(1,'absolute')  # absolute 绝对移动,相对起始位置,往后移动几位
    cursor.scroll(1,'relative')  # relative 相对移动,现对于当前光标所在位置,往后移动几位
    
    print(cursor.fetchall())  # fetchall获取所有数据,返回的是一个列表
    # [{'tid': 1, 'tname': '张磊老师'}, {'tid': 2, 'tname': '李平老师'},
    # {'tid': 3, 'tname': '刘海燕老师'}, {'tid': 4, 'tname': '朱云海老师'},
    # {'tid': 5, 'tname': '李杰老师'}]

    增删改查

    import pymysql
    
    conn = pymysql.connect(
        host = '127.0.0.1',
        port = 3306,
        user = 'root',
        password = 'root',
        database = 'db2',
        charset = 'utf8',
        autocommit = True  # 这个参数配置后,增删改操作都不会需要手动加conn.commit了
    
    )
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    
    '''
    增删改操作对于数据库来说都是敏感操作
    都必须加一句 conn.commit()
    '''
    # conn.commit()
    # sql = 'insert into user(id,name,password) values(3,"dazhuang","321")'  # 增数据
    # sql = 'update user set name = "xiaozhuang" where id = 3'  # 改数据
    # sql = 'delete from user where id = 3'  # 删数据
    # cursor.execute(sql)
    
    username = input('>>>:')
    pwd = input('>>>:')
    sql = "select * from user where name = %s and password = %s"  # 查数据
    print(sql)
    # 根据sql语句匹配用户输入的名合密码是否存在
    res = cursor.execute(sql,(username,pwd))  # 能帮你自动过滤特殊符号,不免sql注入问题
    # execute 能够识别%s并且自动过滤特殊符号 完成sql语句的拼接
    print(res)  # 若存在 返回的是数据的条数1,不存在返回的是0条数据
    # 判断 res 是否有值
    if res:
        print(cursor.fetchall())
    else:
        print('用户名或密码错误')

     sql注入问题

    sql 注入问题:
        就是利用注释等具有特殊意义的符号来利用mysql的漏洞
        
    解决办法:
        利用excute帮你去拼接数据
        
        将 sql = "select * from user where name = %s and password = %s" %(username,pwd)
        改为: sql = "select * from user where name = %s and password = %s" 
                res = cursor.execute(sql,(username,pwd))
    # 不要手动去拼接查询的sql语句
    username = input(">>>:").strip()
    password = input(">>>:").strip()
    sql = "select * from user where username='%s' and password='%s'"%(username,password)
    
    # 用户名正确
    username >>>: jason' -- jjsakfjjdkjjkjs
    # 用户名密码都不对的情况
    username >>>: xxx' or 1=1 --asdjkdklqwjdjkjasdljad
    password >>>: ''
  • 相关阅读:
    一个好的时间函数
    Codeforces 785E. Anton and Permutation
    Codeforces 785 D. Anton and School
    Codeforces 510 E. Fox And Dinner
    Codeforces 242 E. XOR on Segment
    Codeforces 629 E. Famil Door and Roads
    Codeforces 600E. Lomsat gelral(Dsu on tree学习)
    Codeforces 438D The Child and Sequence
    Codeforces 729E Subordinates
    【ATcoder】D
  • 原文地址:https://www.cnblogs.com/waller/p/11396919.html
Copyright © 2011-2022 走看看