SQLite基本语法

来源:互联网 发布:电脑软件开发公司 编辑:程序博客网 时间:2024/05/20 18:41

转载地址:http://www.cnblogs.com/Oldz/p/6388011.html?utm_source=itdadao&utm_medium=referral

命令行操作SQLite

创建一张表

create table 表名(字段名字 字段类型, 字段名字 字段类型);

create table info(id int,name varchar(20));

增:

       insert into 表名 values(要添加的值…);

       当添加的字段的数据类型是int类型,直接写值

       如果添加的字段类型是varchar类型和日期类型,使用单引号把值包起来

       insert into info values(1,'zhangsan','110');

        insert into test (name) values('lisi');

        插入记录的时候可以在表名的后面加上(列名 ) 可以向指定的列中插入数据 

改:

   update 表名 set 字段名1 = 值1 , 字段名2 = 值2 where 条件;

    update info set phone='12345' where name='zhangsan';

删:

      delete from 表名 where 条件 如果没有where条件 删除表中的所有数据

        delete from info where id > 1;

        delete from info; 删除表中的所有数据

查:

     select 字段名 (*) as别名  from 表名 where 条件

      select * from info; 查询所有字段的内容

      select name,phone from info where id = 1;

去除表中重复记录(只会影响查询的结果 不会修改表中的记录)

       select distinct * from 表名;

         select distinct name from info; 只显示名字 去除重复的记录

 

部分查询 : desc 降序 limit 查询的个数,offset 坐标

select number,mode from info order by _id desc  limit ? offset?  

读取通讯录联系人,两个表嵌套 

select location from Data1 where id=(select outkey from Data2 where num=1)

db.execSQL("create table info (_id integer primary key autoincreament,name varchar[20] ,phone varchar [20] )"  )

db.execSQL("alter table info add age integer " ) 插入某列

 

5使用sql对数据库进行操作

创建数据库

create database 数据库名字;

显示所有数据库

show databases;

使用某个数据库

use 数据库的名字;

查看当前使用的数据库是哪一个

select database();

删除数据库

drop database 数据库名字;

 

6 使用sql对表进行操作

创建一张表

create table 表名(字段名字 字段类型, 字段名字 字段类型);

create table info(id int,name varchar(20));

查看表结构

desc 表名字;

desc info;

查看当前数据库有哪些表

show tables;

删除表

drop table 表名;

drop table info;

修改表结构 添加一列

alter table 表名 add 字段名字 字段类型;

alter table info add phone varchar(20);

 

sql支持的数据类型

字符串型 

VARCHAR 可变长度的字符串  varchar(20) 13888888888; 在申请的空间范围内 用多少分配多少

CHAR  char(20);

大数据类型

BLOB 

TEXT 比较大的文本

数值型

TINYINT   byte

SMALLINT short

INT    int

BIGINT long

FLOAT float

DOUBLE double

逻辑性 

BIT  boolean

日期型

DATE 日期

TIME   时间

DATETIME 日期和时间

TIMESTAMP 时间戳

 

 

7使用sql对表中的记录进行操作 CRUD   ☆☆☆☆☆ ☆☆☆☆☆

1.插入记录

       insert into 表名 values(要添加的值…);

       当添加的字段的数据类型是int类型,直接写值

       如果添加的字段类型是varchar类型和日期类型,使用单引号把值包起来

 

        insert into info values(1,'zhangsan','110');

        insert into test (name) values('lisi');

        插入记录的时候可以在表名的后面加上(列名 ) 可以向指定的列中插入数据 

2.修改记录

       update 表名 set 字段名1 = 值1 , 字段名2 = 值2 where 条件;

    update info set phone='12345' where name='zhangsan';

3.删除记录

       delete from 表名 where 条件 如果没有where条件 删除表中的所有数据

        delete from info where id = 1;

        delete from info; 删除表中的所有数据

4.查询记录

       select 字段名 (*) as别名  from 表名 where 条件

      select * from info; 查询所有字段的内容

      select name,phone from info where id = 1;

5.去除表中重复记录(只会影响查询的结果 不会修改表中的记录)

       select distinct * from 表名;

         select distinct name from info; 只显示名字 去除重复的记录

6.MySQL的约束

       a.非空约束  not  null

              * 表示数据不能为空

       b.唯一性约束  unique

              * 表中的记录不能重复的

       c.主键约束  primary key

              * 表示非空,唯一性

         d 自动增长 auto_increment

create table test(id int primary key auto_increment,name varchar(20));

 

8where条件的使用

.where子句使用

       a. 运算符 <, >,  >=,  <=

       b. in 在范围内

select * from student where Android in (70,90);   注意in不是指定起始和结束的范围 而是在()中的数据中做选择

 

 如果想指定起始和结束的范围要使用and 条件

c. and  条件同时满足

 select * from student where Android>=70 and Android<=90;

 

       d. like 模糊查询

        % 表示占位符(通配符) %替换若干文字

      select * from student where name like '%zha%';

     //查询表中名字里含有zha的记录

 

 

 

排序

       order by 字段名 asc   升序

       order by 字段名 desc 降序

         默认升序的 如果想降序 需要加上desc

select * from student order by English;

 

 

9聚合函数,分组查询&limit关键字

1.count()函数

       select count(*) from ...where....

2.sum()函数

       select sum(要进行求和字段) from ...where.... 

3. avg()函数

       select avg(要计算平均数的字段名称) from …

4. max()函数 

       select max(字段) from...

5. min()函数 

       select min(字段) from... 

分组查询 group by

 

 

select name,sum(price) from orders group by name;

按照名字进行分组 查询每种商品的总价格

 

 分组查询如果加上条件不能使用where 而要使用having

 select name,sum(price) from orders group by name having sum(price)>5000;

 

limit

 select * from orders limit 2; 从第一条开始显示 显示2条

select * from orders limit 2,3; 从第2+1条开始显示 显示3条记录;

通过limit关键字 可以实现数据的分页查询(只查询一部分数据)


原创粉丝点击