• Navicat使用和pymysql


    回顾

    # 完整的语法结构
    select distinct * from 表名
    	where 分组之前的过滤条件
    	group by 分组依据
    	having 分组
    	order by 字段1 asc,字段2 desc
    	limit 5,5 从第五条开始往后展示五条
    
    #where 后面的过滤条件可以跟判断符号(>,<,in ,not in,=),(注意判断是否为null的时候不能用=,只能用is)可以跟模糊判断(like '%',_标识任意一个字符)
    # char_length(字段名):查看字段中数据的长度
    # concat 连接字符
    select concat(name,':',age) from emp;
    # 如果连接符号都一样的话可以用concat_ws
    select concat_ws(':',name,age,post) from emp;
    # group_concat 分组之后进行连接
    
    # 多表查询
    inner join  # 内连接
    left join   # 左连接
    right join  # 右连接
    union       # 全连接
    

    补充(了解)

    exist
    EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录,
    而是返回一个真假值,True或False。
    当返回True时,外层查询语句将进行查询
    当返回值为False时,外层查询语句不进行查询。
    select * from emp where exists (select * from dep where 1=2);    # Empty set (0.01 sec)
    select * from emp where exists (select * from dep where id >1);
    
    
    # 需要掌握的
    #1.测试+链接数据库
    #2.新建库
    #3.新建表,新增字段+类型+约束
    #4.设计表:外键
    #5.新建表查询
    #6.建立表模型
    
    #注意:
    批量加注释:ctrl+?键
    批量去注释:Ctrl+shift+?键
    

    pymysql模块

    # 1.安装:pip3 install pymysql
    # 2.代码链接
    import pymysql
    #链接
    conn = pymysql.connect(
    	host='localhost',
    	user='root',
    	password='123',
    	database='lucas',
    	charset='utf8',
    	autocommit=True)
    # 游标
    cursor=conn.cursor() #执行完毕返回的结果默认以元组显示
    #cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) 以字典的方式 显示数据
    
    # 3.pymysql操作数据库
    # 执行sql语句
    user=input('>>>:').strip()
    pwd=input('>>>:').strip()
    sql='select * from userinfo where name="%s" and password="%s"'%(user,pwd) # 注意%s需要加引号
    
    rows = cursor.execute(sql) # 执行sql语句,返回sql查询成功的记录数目
    
    # 获取真实数据
    res = cursor.fetchone() # 获取一条
    res = cursor.fetchall() # 获取所有
    res = cursor.fetchmany() # 获取多条
    
    cursor.scroll(1,'relative') #相对移动,游标从当前位置往后移动一个
    cursor.scroll(1,'absolute') #绝对移动,游标从头开始往后移动1个
    
    cursor.close()
    conn.close()
    
    

    sql注入

    # 不要手动去拼接查询的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 >>>: ''
    #以上两种情况都能进入数据库,所以应该由数据库去判断
    username = input(">>>:").strip()
    password = input(">>>:").strip()
    sql = "select * from user where username=%s and password=%s"
    rows = cursor.execute(sql,(username,password))
    
    

    增删改

    #增
    sql = "insert into user(username,password) values(%s,%s)"
    rows = cursor.excute(sql,('lucas','123'))
    
    #修改
    sql = "update user set username='ymg' where id=1"
    rows = cursor.excute(sql)
    
    #一次插入多行记录
    rows=cursor.excutemany(sql,[(),(),()])
    
    #增和改单单执行excute并不会真正影响到数据,需要再执行conn.commit()才可以完成真正的增改,也可以直接在建链接的时候加上:autocommit=True
    
    
    
    
  • 相关阅读:
    前端笔记-css sprite,font+html,font+css
    Python基础教程(第2版•修订版)代码清单2-3 勘误
    Python基础教程(第2版•修订版)代码清单2-3 勘误
    程序员健康Tips
    程序员健康Tips
    WAMP安装,期间提示丢失VCRUNTIME140.dll
    WAMP安装,期间提示丢失VCRUNTIME140.dll
    安装Vmware时竟然也会报错,错误信息见图
    安装Vmware时竟然也会报错,错误信息见图
    无符号数tips
  • 原文地址:https://www.cnblogs.com/yanminggang/p/10876056.html
Copyright © 2020-2023  润新知