码迷,mamicode.com
首页 > 数据库 > 详细

SQLite

时间:2015-04-12 09:16:39      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:sqlite

在程序运行过程中,要想操作(增删改查,CRUD)数据库中的数据,必须使用SQL语句

selectinsertupdatedeletefromcreatewheredescorderbygrouptablealterviewindex



SQL语句的种类

数据定义语句(DDLData Definition Language

包括createdrop等操作

在数据库中创建新表或删除表(create table或 drop table


数据操作语句(DMLData Manipulation Language

包括insertupdatedelete等操作

上面的3种操作分别用于添加、修改、删除表中的数据


数据查询语句(DQLData Query Language

可以用于查询获得表中的数据

关键字selectDQL(也是所有SQL)用得最多的操作

其他DQL常用的关键字有whereorder bygroup byhaving

创表

格式

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

create table if not exists 表名 (字段名字段类型1, 字段名字段类型2, …) ;


示例

create table t_student (id integer, name text, age inetger, score real) ;



字段类型



SQLite将数据划分为以下几种存储类型:

integer : 整型值

real : 浮点值

text : 文本字符串

blob : 二进制数据(比如文件)


实际上SQLite是无类型的

就算声明为integer类型,还是能存储字符串文本(主键除外)

建表时声明啥类型或者不声明类型都可以,也就意味着创表语句可以这么写:

create table t_student(name, age);


为了保持良好的编程规范、方便程序员之间的交流,编写建表语句的时候最好加上每个字段的具体类型



删表



格式

drop table 表名 ;

drop table if exists 表名 ;


示例

drop table t_student ;


插入数据(insert)



格式

insert into 表名 (字段1, 字段2, …) values (字段1的值字段2的值, …) ;


示例

insert into t_student (name, age) values (‘mj’, 10) ;


注意

数据库中的字符串内容应该用单引号  括住



更新数据(update)



格式

update 表名 set 字段= 字段1的值字段= 字段2的值, … ; 


示例

update t_student set name = ‘jack’, age = 20 ; 


注意

上面的示例会将t_student表中所有记录的name都改为jackage都改为20



删除数据(delete)



格式

delete from 表名 ;


示例

delete from t_student ;


注意

上面的示例会将t_student表中所有记录都删掉



条件语句


如果只想更新或者删除某些固定的记录,那就必须在DML语句后加上一些条件


条件语句的常见格式

where 字段 = 某个值 ;   // 不能用两个 =

where 字段 is 某个值 ;   // is 相当于 

where 字段 != 某个值 ; 

where 字段 is not 某个值 ;   // is not 相当于 != 

where 字段 > 某个值 ; 

where 字段= 某个值 and 字段> 某个值 ;  // and相当于C语言中的 &&

where 字段= 某个值 or 字段= 某个值 ;  //  or 相当于C语言中的 ||



条件语句练习



示例

t_student表中年龄大于10 并且 姓名不等于jack的记录,年龄都改为 5

update t_student set age = 5 where age > 10 and name != ‘jack’ ;


删除t_student表中年龄小于等于10 或者 年龄大于30的记录

delete from t_student where age <= 10 or age > 30 ;


猜猜下面语句的作用

update t_student set score = age where name = ‘jack’ ;

t_student表中名字等于jack的记录,score字段的值 都改为 age字段的值


DQL语句



格式

select 字段1, 字段2, … from 表名 ;

select * from 表名;   //  查询所有的字段


示例

select name, age from t_student ;

select * from t_student ;

select * from t_student where age > 10 ;  //  条件查询



起别名

格式(字段和表都可以起别名)

select 字段1 别名 , 字段2 别名 , … from 表名 别名 ; 

select 字段别名字段as 别名, … from 表名 as 别名 ;

select 别名.字段1, 别名.字段2, … from 表名 别名 ;


示例

select name myname, age myage from t_student ;

name起个叫做myname的别名,给age起个叫做myage的别名


select s.name, s.age from t_student s ;

t_student表起个别名叫做s,利用s来引用表中的字段


计算记录的数量



格式

select count (字段from 表名 ;

select count ( * ) from 表名 ;


示例

select count (age) from t_student ;

select count ( * ) from t_student where score >= 60;



排序


查询出来的结果可以用order by进行排序

select * from t_student order by 字段 ;

select * from t_student order by age ;


默认是按照升序排序(由小到大),也可以变为降序(由大到小)

select * from t_student order by age desc ;  //降序

select * from t_student order by age asc ;   // 升序(默认)


也可以用多个字段进行排序

select * from t_student order by age asc, height desc ;

先按照年龄排序(升序),年龄相等就按照身高排序(降序)



limit



使用limit可以精确地控制查询结果的数量,比如每次只查询10条数据


格式

select * from 表名 limit 数值1, 数值2 ;


示例

select * from t_student limit 4, 8 ;

可以理解为:跳过最前面4条语句,然后取8条记录



SQLite

标签:sqlite

原文地址:http://blog.csdn.net/guoyule2010/article/details/45000049

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!