zoukankan      html  css  js  c++  java
  • python 连接mysql数据库

    1.python 连接的mysql数据库

    +------+
    | id   |
    +------+
    |    1 |
    |    7 |
    |    3 |
    |    4 |
    |    5 |
    |    8 |
    |    9 |
    |   13 |
    |   12 |
    |   14 |
    |   16 |
    +------+
    上面这个是mysql里面的 表stu_2
    
    
    下面可以进行数据的增删改查
    import pymysql
    conn = pymysql.connect(
        user = 'root',
        password = '123456',
        host = '127.0.0.1',
        port = 3306,
        charset = 'utf8',
        database = 'a1',
        autocommit = True #自动提交给mysql 确认
        #这个不写 也可以在最下方 写 conn.commit() 确认当前的操作,真正同步到数据库
    )
    cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)##产生的是一个游标对象
    #cursor = pymysql.cursors.DictCursor 将查询的结果制作成字典的形式返回
    # sql = 'show databases'
    sql = 'select * from stu_2'
    res = cursor.execute(sql)  #执行的是sql的语句 
    print(res)#返回的当前的sql影响的行数 >>>11
    # ret = cursor.fetchone() #只获取查询结果的第一条数据 {'id': 1}
    # ret = cursor.fetchall()# 获取查询结果的所有数据
    >>>>>>>>>>>>>>>>>>
    [{'id': 1}, {'id': 7}, {'id': 3}, {'id': 4}, {'id': 5}, {'id': 8}, {'id': 9}, {'id': 13}, {'id': 12}, {'id': 14}, {'id': 16}]
    ret = cursor.fetchmany(2)#指定获取几条数据,如果数字超出了,也不报错[{'id': 1}, {'id': 7}]
    print(ret)
    #相对移动
    # cursor.scroll(2,'relative') #基于指针所在位置 往后偏移 2位
    [{'id': 5}, {'id': 8}, {'id': 9}, {'id': 13}, {'id': 12}, {'id': 14}, {'id': 16}]
    cursor.scroll(3,'absolute')#基于起始位置 往后偏移
    >>>>>>>>>>>>>>>>>
    [{'id': 4}, {'id': 5}, {'id': 8}, {'id': 9}, {'id': 13}, {'id': 12}, {'id': 14}, {'id': 16}]
    print(cursor.fetchall())
    

    2.sql注入问题

    import pymysql
    conn = pymysql.connect(
        user = 'root',
        password = '123456',
        db = 'a1',
        host = '127.0.0.1',
        port = 3306,
        charset = 'utf8'
    )
    cursor_obj = conn.cursor(cursor= pymysql.cursors.DictCursor)
    #获取用户输入的用户名和密码,然后进行校验
    username= input('姓名:').strip()
    password = input('密码:').strip()
    #sql = "select * from stu_3 where name = '%s' and password = '%s'"%(username,password)##这样 会巧妙绕过真正的sql校验
    #>>>>>>>>姓名:xxxx' or 1 = 1 -- hflekelel  可以打印出正常结果
    sql = "select * from stu_3 where name = %s and password = %s"
    
    
    
    cursor_obj.execute(sql,(username,password))
    res = cursor_obj.fetchall()
    if res:
        print(res)
    else:
        print('姓名或者密码错误')
    #sql 的 注入问题
    #利用特殊符号和注释语法,巧妙的绕过真正的sql校验 -- dsdgg --+空格是mysql的注释
    #关键性的数据,不要自己手动去拼接,而是 给execute 帮你做拼接
    
  • 相关阅读:
    .net web mvc 权限验证
    .net web api 权限验证
    LeetCode-1021 Remove Outermost Parentheses Solution(with Java)
    LeetCode-682 Baseball Game Solution (with Java)
    LeetCode-859 Buddy Strings Solution (with Java)
    LeetCode-917 Reverse Only Letters Solution (with Java)
    LeetCode-521 Longest Uncommon Subsequence I Solution (with Java)
    LeetCode-937 Reorder Data in Log Files Solution (with Java)
    LeetCode-1 Two Sum Solution (with Java)
    LeetCode-985 Sum of Even Numbers After Queries Solution (with Java)
  • 原文地址:https://www.cnblogs.com/bs2019/p/12102016.html
Copyright © 2011-2022 走看看