zoukankan      html  css  js  c++  java
  • 多表查询,笛卡尔积 ,可视化工具,pymysql查询与增删改 ,sql 注入













    sql 注入

    import pymysql
    conn=pymysql.connect(host='localhost',port=3306,
    db='db1',user='user',password='root')
    cur=conn.cursor(cursors.DictCursor)
    name=input('please input name:')
    password=input('please input password')
    sql='select *from user where user="%s" and password="%s"'%(name,password)
    res=cur.execute(sql)

    if res:
    print('login success’)
    这样执行的话引用注释可用越过登录验证
    1.知道用户名: # -- zzf" -- h
    677
    2.不知道:yy" or 2=2 --e
    567

    防止措施
    sql='select *from user where user=%s and password=%s'
    res=cur.execute(sql,(name,password))


    增加字段内容
    import pymysql倒入模块
    coon=pymysql.connect(host='localhost',port=3306,db='db1',user='root',password='root')建立连接
    cur=coon.cursor(pymysql.cursors.DictCursor)启用游标
    sq='insert into em (id,name)values (%s,%s)'
    res=cur.execute(sq,(8,"hh")) 书写和执行sql 语句
    coon.commit() 将内容写入内存
    cur.close()
    coon.close()


    删除内容
    import pymysql倒入模块
    coon=pymysql.connect(host='localhost',port=3306,db='db1',user='root',password='root')建立连接
    cur=coon.cursor(pymysql.cursors.DictCursor)启用游标
    sq='delete  from em where id=%s'
    cur.execute(sq,2)
    coon.commit()

    修改字段内容
    import pymysql倒入模块
    coon=pymysql.connect(host='localhost',port=3306,db='db1',user='root',password='root')建立连接
    cur=coon.cursor(pymysql.cursors.DictCursor)启用游标
    sql=‘ update em set name=%s where id=%s’
    cur.execute(sql,("hh",5)
    coon.commit()


    查找内容(***)
    import pymysql
    conn=pymysql.connect(host='localhost',port=3306,db='db1',user='root',
    password='root')
    cur=conn.cursor(pymysql.cursors.DictCursor)
    sql='select * from em '
    res=cur.execute(sql)
    cur.scroll(4,mode='absolute')
    tag=cur.fetchall()
    for i in tag:
    print(i['name],i['salary'])
    cursor.scroll(1, mode='relative') # 指针相对于上一次位置往后偏移1条记录
    # cursor.scroll(res - 1, mode='absolute') # 指针绝对, 游标永远从头开始偏移





    多表查询

    内连接

    ```

    内连接:结果为两张表有对应关系的数据

    语法:左表 inner join 右表 on 两表有关联的字段的条件

    eg:select * from emp inner join dep on emp.dep_id = dep.id;

    ```

    左连接

    ```

    左连接:在内连接的基础上还保留左表的记录

    语法:左表 left join 右表 on 两表有关联的字段的条件

    eg:select * from emp left join dep on emp.dep_id = dep.id;

    ```

    右连接

    ```

    右连接:在内连接的基础上还保留右表的记录

    语法:左表 right join 右表 on 两表有关联的字段的条件

    eg:select * from emp right join dep on emp.dep_id = dep.id;

    ```

    全连接

    ```

    全连接:在内连接的基础上分别保留这左表右表的记录

    语法:mysql没有full join on语法,但可以通过去重达到效果

    eg:

    select * from emp left join dep on emp.dep_id = dep.id

    union

    select * from emp right join dep on emp.dep_id = dep.id;

    ```

    练习

    ```

    1.查询每一位员工对应的工作职责

    select emp.name, dep.work from emp left join dep on emp.dep_id = dep.id;

    2.查询每一个部门下的员工们及员工职责

    select max(dep.name), group_concat(emp.name), max(dep.work) from emp right join dep on emp.dep_id = dep.id group by dep_id;

    ```






  • 相关阅读:
    mysql操作规范
    在线修改大表结构pt-online-schema-change
    MySQL 过滤复制
    IDEA “Cannot resolve symbol” 解决办法
    JAVA NIO Selector Channel
    Mysql二级索引
    技术选型关于redis客户端选择
    知乎上看到的一篇讲解Epoll的文章,较形象生动
    linux安装运行virtuoso数据库的详细过程
    go安装步骤(linux和Windows)
  • 原文地址:https://www.cnblogs.com/wrqysrt/p/10257305.html
Copyright © 2011-2022 走看看