zoukankan      html  css  js  c++  java
  • python day11 学习整理

    1 mysql

    1.1 mysql 语句

    1.1.1 文件夹操作(数据库)

    1 创建:
    2     create database db_name;
    3     create database db_name default charset utf8;
    4 删除:
    5     drop database db_name;
    6 进入文件夹:
    7     use db_name 

    1.1.2 文件操作(数据表)

    创建:
          单表:
            create table user_info(
            id int not null auto_increment primary key,
            name char(20),
            age int,
            gender char(1)
            )engine = innodb default charset=utf8;
    
                    insert into user_info(name,age,gender) values('alex',19,'');
    
    
            1. 列名
            2. 数据类型
            3. 是否可以问空(null,not null)
            4. 默认值(default 1)
            5. 自增(auto_increment)   一个表只能有一个自增列
            6. 主键(primary key)  
                约束:数据不能为空、数据不能重复。
                索引:加速查找(B树结构)
            7. 外键:
                对某列数据的内容进行约束。
                约束:只能是某个表中某列已经存在的数据。
                constraint 约束名称 foreign key(deparment_id) references dep_info(id)
                   含外键的关系成为1对多。
    
    
        多表:
            一对多:
                create table user_info(
                id int not null auto_increment primary key,
                name char(20),
                age int,
                gender char(1),
                deparment_id int,
                constraint 约束名称 foreign key(deparment_id) references dep_info(id))engine = innodb default charset=utf8;
    
                            create table dep_info(
                id int not null auto_increment primary key,
                title char(32),
                )engine=innode default charset=utf8;
                        
            多对多:(关系表)
                create table boy(
                id int not null auto_increment primary key,
                name char(32))engine = innodb default charset=utf8;
                            
                create table girl(
                id int not null auto_increment primary key,
                name char(32))engine = innodb default charset=utf8;
                            
                create table b2g(
                id int not null auto_increment primary key,
                b_id int,
                g_id int,
                constraint 约束名称1 foreign key(b_id) references boy(id),
                constraint 约束名称2 foreign key(g_id) references girl(id))engine = innodb default charset = utf8;
    删除:
        drop table table_name;

    1.1.3 应用场景

    1、根据具体业务
    2、HTML的场景
          创建的时候只需要输入数据的情况下:单表
          创建的时候必须要我们选择一个其他数据的场景下:一对多
          创建的时候针对某些字段可以进行多选的情况下:多对多

     1.1.4 文件内容操作(数据行)

                插入:
                    insert into  table_name(field) values(value),(value2)  --> 两个(value)表示插如多行数据
                    insert into  table_name(cname) select field from table_name  -->  把select查到的结果,当作数据来赋值给value
                删除:
                    清空表:
                        delete from table_name  --> 自增列会继续之前的ID
                        truncate table table_name  --> 物理删除,速度快,重新计算ID
                    删除某一条:
                        delete from table_name where filed = values and/or ...  --> 只删除符合条件的数据
                        delete from table_name where filed in (1,2,3,4)  
                        delete from table_name where id between 5 and 10 
                修改:
                    update table_name set field = 'value'  --> 更新所有数据的field字段的值,加 where 只修改匹配到的行
                    update table_name set id = 8 , name = 'daxin' where age = 18;
                查询:
                    select * from table_name where id > 2
                    select field as '别名' from table_name  --> 加别名
                    select * from table_name where id in (1,2)
                    select * from table_name where cid in (select tid from teacher)
                    条件:
                        select * from table_name order by field asc/desc(正序/倒序)
                        select * from table_name order by field asc limit 1 取第一个值
                        select * from table_name limit 1,2(起始位置,找几个)
                        select * from table_name where field like '%key%'  --> 查找field字段包含key的数据
                            % 表示任意个任意字符,  _表示任意一个字符
                    分组:
                        select * from table_name group by field  --> 分组显示,会去重,需要使用聚合函数来统计重复的次数
                        select field,count(id) from table_name group by field --> 对id字段进行聚合(其他的还有min(),max(),sum(),avg()等)
                        例子:
                            1、获取每个班级多少人
                                SELECT  class.caption,count(sid) from class
                                LEFT join student on student.class_id = class.cid 
                                group by class.caption
                            2、获取每个班级有多少人并且选出认识大于2的班级,
                                注意:如果针对 group by 的结果进行筛选,那么需要使用 having 不能在使用 where 了.
                                SELECT  class.caption,count(sid) as number from class
                                LEFT join student on student.class_id = class.cid 
                                group by class.caption
                                HAVING number >= 2
                            3、每个课程的不及格的人数。
                                select course.cname,count(sid) from score 
                                left join course on score.corse_id = course.cid
                                where number < 60 
                                group by course.cname
    
                    连表:
                        select student.sid,student.sname,class.caption from student LEFT JOIN class on student.class_id = class.cid ;
                            把class表中的字段放在student表的左边,并且进行 student.class_id = class.cid 匹配后显示,数据量以from指定的表为基准
                            left join:
                                以 from 指定的表为基准,对数据进行显示
                            right join:  -->不常用join 后面的表为基准进行显示。
                            inner join:(join 使用的就是)
                                只保留连个表中都有数据的条目
                        例子:
                            1、id=1的老师任教的课程名称
                            2、老师姓名瞎猫任教的课程命令
                            3、已选课程id=1,所有学生的姓名
                            4、已选体育课,所有学生的姓名
                            5、已选波多任教任意课程,所有学生姓名
    
                        union:
                            把两个SQL的结果进行组合(上下合并)
                                select * from student 
                                union / union all 
                                select * from teacher;
                                注意上下两个表中的列数要统一
                            注意:
                                1、如果所有数据都一致,那么union会对结果进行去重
                                2union all ,会保存所有的

    1.2 基本数据类型

    MySQL的数据类型大致分为:数值、时间 和 字符串。
        数字:
            整数
                tinyint 小整数,数据类型用于保存一些范围的整数数值范围。
                smallint
                int
                bigint
            小数 
                float 浮点型(长度越长越不精准)  
                double 浮点型(双精度,精度比float稍高)   范围比float更大
                decimal 精准(内部使用字符串进行存储的)   ->  适合对精度有要求的
        字符串
            char(19)[字符长度]     定长字符串 --> 占用空间大,但是效率高
            varchar(19)[字符长度]  不定长字符串  --> 占用空间是可变的,但是效率低
            注意:最大可以存放255个字符
            text()  65535个字符
            mediumtext()  16777215个字符
            longtext()  4294967254个字符
            二进制:
                TinyBlob
                Blob 
                MediumBlob
                LongBlob
                存文件:虽然可以用二进制进行存储,但是一般是存储文件在服务器上的路径(URL)
        时间:
            date  YYYY-MM-DD
            time  HH:MM:SS
            year  YYYY
            DATETIME YYYY-MM-DD HH:MM:SS  -->常用
            TIMESTAMP 时间戳格式

    2 python 操作数据库

    连接数据库

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    import pymysql
    
    conn = pymysql.Connect(host='192.168.175.131',port=3306,user='root',password='123456',database='stupy',charset='utf8')
    cursor = conn.cursor()
    res = cursor.execute('select * from student') #受影响的行数
    # print(res)
    res1 = cursor.fetchall() #通过游标去拿到所有数据
    # res1 = cursor.fetchone() #通过游标去拿到一条数据
    #res1 = cursor.fetchmany(3) #通过游标去拿到三条数据
    
    print(res)
    cursor.close()
    conn.close()

    增删改

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    import pymysql
    
    conn = pymysql.Connect(host='192.168.175.131',port=3306,username='root',password='123456',database='stupy',charset='utf8')
    cursor = conn.cursor()
    v = cursor.execute('insert into userinfo (username,password) values (%s,%s)',['eric','9999'])
    ##v = cursor.execute('delete username from userinfo where username = %s', ['eric'])
    #v = cursor.execute('update userinfo set username = %s where username = %s ', ['eric666','eric'])
    conn.commit() cursor.close() conn.close()

    查询数据格式

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    import pymysql
    
    conn = pymysql.Connect(host='192.168.175.131',port=3306,user='root',password='123456',database='stupy',charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  #修改查询出来的值得格式
    cursor.execute('select * from student')
    res = cursor.fetchall()
    print(res)
    cursor.close()
    conn.close()

    用户登录

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    import pymysql
    user = input('请输入用户名:').strip()
    pwd = input('请输入密码:').strip()
    
    conn = pymysql.Connect(host='192.168.175.131',port=3306,user='root',password='123456',database='stupy',charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    # sql = 'select * from userinfo where username = "%s" and password = "%s"' %(user,pwd,) 这样容易sql注入,所以不推荐这么写。pymysql帮你处理了sql注入
    v = cursor.execute('select * from userinfo where username= %s and password = %s',[user,pwd])
    res = cursor.fetchone()
    print(res)
    cursor.close()
    conn.close()

    两张表操作

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    import pymysql
    
    conn = pymysql.Connect(host='192.168.175.131',port=3306,user='root',password='123456',database='stupy',charset='utf8')
    cursor = conn.cursor()
    
    cursor.execute('insert into class (caption) VALUES (%s)',['新班级'])
    conn.commit()
    new_class_id = cursor.lastrowid #获取新增数据自增id
    cursor.execute('insert into student (sname,gender,class_id) VALUES (%s,%s,%s)',['sdfsd','',new_class_id])
    conn.commit()
    
    cursor.close()
    conn.close()
  • 相关阅读:
    个人作业2——英语学习APP案例分析
    结对编程1—— 基于界面的四则运算(38/39)
    个人作业1——四则运算题目生成
    软件工程实践项目课程的自我目标
    IE6/IE7/IE8/Firefox/Chrome/Safari的CSS hack兼容一览表
    微信小程序爬坑日记之蜜汁缩进
    微信小程序爬坑日记之背景图片设置
    你不知道的 js 保留字
    微信小程序爬坑日记之下拉刷新
    ES7-Es8 js代码片段
  • 原文地址:https://www.cnblogs.com/ybyblog/p/7168828.html
Copyright © 2011-2022 走看看