美文网首页
Python进阶2 - 数据库编程: SQLite入门

Python进阶2 - 数据库编程: SQLite入门

作者: 小马哥China | 来源:发表于2019-06-09 20:01 被阅读0次

    主要内容:

    • DDL类语句
      1. 创建表
      2. 删除表
    • DML类语句
      数据的增删改查语句

    DDL类

    1,数据库管理命令

    查看数据库中的可用数据库
    .database/.databases
    查看数据库中的表
    .tables 
    查看表的完整信息
    .schema company
    编程的时候就不能用点命令了
    select tb1_name from sqlite_master where type = 'table'
    显示命令:
    .header on      打开字段名
    .mode column    每个列10个字符的宽度
    .mode width     各个列的宽度可以自定义设置
    .mode line      打开行模式
    

    2,创建表

    create table company(
        id int primary key not null,
        name text not null,
        age int not null,
        address char(50),
        salary real
    );
    
    create table DEPARTMENT(
        id int primary key not null,
        dept char(50) not null,
        emp_id int not null
    );
    
    

    3,删除表

    drop table databse_name.table_name
    

    DML类操作

    1,插入数据

        insert into table_name [] values ();
        为所有字段添加值,可以省略掉指定列名称的命令
        insert into company (id,name,age,address,salary) 
        values (1,'mayun',55,'hangzhou',20000.00);
    
        insert into company (id,name,age,address,salary)
        values (2,'mahuateng',49,'shenzhen',55000);
    
        insert into company (id,name,age,address,salary)
        values (3,'renzhengfei',70,'shenzhen',10000);
    
        insert into company (id,name,age,address,salary)
        values(4,'liyanhong',48,'shanxi',30000);
    
        insert into company (id,name,age,address,salary)
        values(5,'mali',34,'hebei',1000000.00);
    
        insert into company values (6,'xxx',18,'China',10000.00);
    
        还可以通过使用其它表来填充另一个表
    

    2,查看数据

        select * from tableName;
        select column1,column2,...,columnN from tableName;
        
        where条件
            where子句用于指定从一个表或者多个表中获取数据的条件
            如果满足给定的条件,为真,则从表中返回特定值,用于过滤记录
            一般使用比较运算符或者逻辑运算符指定过滤条件
            实例: 
            select * from company where age>=25 and salary >10000.00;
            select * from company where age>=25 and salary >20000.00
            select * from company where age is not null;
            select * from company where name like 'ma%%';
            select * from company where name glob 'li_____'   *号能在glob里面使用
            select * from company where age in (55,34);
            select * from company where age between 1 and 60;
            select age from company 
            where exists (select age from company where salary>10000.00);
            条件查询: 通过子查询,给了age一个条件,还有另外一种写法
    
            select * from company
            where age >(select age from company where salary >10000.00);
    
            模糊查询like: %(多个字符) _(单一字符)的使用
            select column_list from tableName where column like '通配符模板';
            模糊查询glob: *(多个字符) ?(单一字符)的使用  (大小写敏感)
    
            limit限制提取的行数
            offset 在哪个位置开始取
    
            排序: order by 
                select column_list from table_name
                where condition
                order by column1,column2,...,columnN ASC|DESC
            
            group by查询:
                放在where条件后,order BY子句之前
                目的是使用函数对分组后的记录做汇总
            having过滤查询:
                select column_list
                from where
                group by
                having #注意这里位置
                order by
                实例: 
                    select * from company 
                    group by name
                    having count(name)<6
    
                    select * from company
                    group by name
                    having count(name)>6
            去重查询:
                distinct
    
                select distinct column1,column2,...,columnn
                from table_name
                where condition
    

    3,修改数据

        update table_name 
        set column1=value1,column2=value2,...,columnN = vlaueN
        [where condition];
        实例:
            update company set address='China' where id=5;
    

    4,删除数据

        delete from company where id = 6;
        delete from company;   #删除全部
    

    3,运算符

        算数运算符: + - * / %
        比较运算符: 
            == = 
            !=  <>
            < > >= <= !< !>
        逻辑运算符:
            and
            or
            not
    
            between...and...: 最大值和最小值范围内的搜索值
            in: 某个值与一系列指定列表的值比较
            not in: 
            like: 某个值域通配运算符的相似值你行比较
            glob: 同like,不过是大小写敏感的
            is  : =
            is not: !=
            ||: 连接运算符
            unique: 搜索指定表中每一行,确保唯一性
            exists: 在满足一定条件的指定表中搜索行的存在
    
            
        位运算符
            |
            &
            ~
            <<
            >>
            select 1|0
    

    4,表达式

        布尔表达式
            where之后的条件
        数值表达式
            select (1+1)
            select 1+1;
            select (1+1) as addxxxxxx;    注意不能用add
        日期表达式
            select current_timestamp;
    

    小马哥正在针对Python的所有常见知识进行汇总,更会有大量实战项目不断补充进来.
    点击-->全栈工程师养成---Python内容导航页<--查看所有Python内容

    相关文章

      网友评论

          本文标题:Python进阶2 - 数据库编程: SQLite入门

          本文链接:https://www.haomeiwen.com/subject/ogonxctx.html