【SQLite】 数据库的相关命令集合

SQLite是一种嵌入式数据库,它的数据库就是一个文件。由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成。

命令操作(Linux环境)

1, 数据库的安装

     sudo apt-get install sqlite

2, 数据库命令:


      1)系统命令 , 都以'.'开头

         .exit 
         .quit
         .table   查看表
         .schema  查看表的结构


          
      2)sql语句, 都以‘;’结尾

        1-- 创建一张表
            create table stuinfo(id integer, name text, age integer, score float);
        
        2-- 插入一条记录
            insert into stuinfo values(1001, 'zhangsan', 18, 80);
            insert into stuinfo (id, name, score) values(1002, 'lisi', 90);

        3-- 查看数据库记录
            select * from stuinfo;
            select * from stuinfo where score = 80;
            select * from stuinfo where score = 80 and name= 'zhangsan';
            select * from stuinfo where score = 80 or name='wangwu';
            select name,score from stuinfo;  查询指定的字段
            select * from stuinfo where score >= 85 and score < 90;

        4-- 删除一条记录
            delete from stuinfo where id=1003 and name='zhangsan';

        5-- 更新一条记录
            update stuinfo set age=20 where id=1003;
            update stuinfo set age=30, score = 82 where id=1003;

        6-- 删除一张表
            drop table stuinfo;

        7-- 增加一列
            alter table stuinfo add column sex char;

        8-- 删除一列
            create table stu as select id, name, score from stuinfo;
            drop table stuinfo;
            alter table stu rename to stuinfo;

     PS:如果要讲数据库的id字段设置主键,并指定自增长类型需要添加primary key 和 autoincrement这连个类型

     create table info(id integer primary key autoincrement, name vchar);

猜你喜欢

转载自blog.csdn.net/feit2417/article/details/81877090
今日推荐