sqlite3的语句记录

安装使用步骤

1.下载sqlite3.exe

2.sqlit3.exe拷贝到windows/system32目录下(也可以直接双击软件打开创建数据库,拷贝方便在电脑的任何位置都能使用)

3.启动命令行窗口

4.输入命令sqlite3

 

名称的由来

SQL:结构化查询语言Structured Query Language

Lite:简化的

-->SQLite:自包含(只有一个文件),无服务器,零配置,事务性SQL引擎。

 

使用SQLite

.开头的是sqlite3软件特有的指令。SQL语句不用.开头,但以;结束。

打开/创建 数据库的2种方式:在电脑的哪个位置执行下面的语句就会在该路径下生成一个数据库文件(不存在该数据库的前提下)

1.dos下运行命令:sqlite3 mytest.db 启动sqlite3 并且设定开发的数据库文件,如果不存在且建表了则会创建.

2.先运行sqlite3,进入后输入 .open mytest.db   该文件会放在命令行所在目录下, 没有该文件时会自动创建, 即使未创建任何表.

 

Sqlite3常用命令

.open mytest.db 打开数据库   mytest.db数据库名,一次只能打开一个库文件

.tables 显示所有表

.schema users 显示表结构 users表名.但这是显示创建表的建表语句。与mysql有区别,并不是显示表结构。

.databases 列出连接的数据库(一个)

.echo ON|OFF 设置命令回显(ON表示回显输入的语句指令等)

.exit 退出本程序

.quit 退出本程序

.help 显示帮助

.read FILENAME 加载执行sql脚本,例如my.sql

.show 显示环境变量信息

 

 

SQLite常用的数据类型

 

INTEGER:带符号的整型,具体取决有存入数字的范围大小。

REAL:浮点数字,存储为8-byte IEEE浮点数。

TEXT:字符串文本。

BLOB:二进制对象。

 

 smallint 16 位元的整数。

 interger 32 位元的整数。

 decimal(p,s) p 精确值和 s 大小的十进位整数,精确值p是指全部有几个数(digits)大小值,s是指小数点後有几位数。如果没有特别指定,则系统会设为 p=5; s=0

 float  32位元的实数。

 double  64位元的实数。

 char(n)  n 长度的字串,n不能超过 254

 varchar(n) 长度不固定且其最大长度为 n 的字串,n不能超过 4000

 graphic(n) char(n) 一样,不过其单位是两个字元 double-bytesn不能超过127。这个形态是为了支援两个字元长度的字体,例如中文字。

 vargraphic(n) 可变长度且其最大长度为 n 的双字元字串,n不能超过 2000

 date  包含了 年份、月份、日期。

 time  包含了 小时、分钟、秒。

 timestamp 包含了 年、月、日、时、分、秒、千分之一秒。

 

实际上:SQLite是无类型的. 这意味着你可以保存任何类型的数据到你所想要保存的任何表的任何列中, 无论这列声明的数据类型是什么(只有自动递增Integer Primary Key才有用). 对于SQLite来说对字段不指定类型是完全有效的. :

Create Table ex3(a, b, c);

即使SQLite允许忽略数据类型, 但是仍然建议在你的Create Table语句中指定数据类型. 因为数据类型对于你和其他的程序员交流, 或者你准备换掉你的数据库引擎是非常有用的.

SQL语句初步

创建表userscreate table users(_id integer primary key autoincrement,   name varchar(30));

primary key:主键.非空的,并且可以唯一标识一条记录的这个字段(或字段组合)

删除表:drop table users;

ALTER TABLE 语句:

追加新的列

语法格式:

ALTER  TABLE   <表名>

ADD [COLUMN]   <字段名>  datatype [DEFAULT expr];

alter table employees add  job_id integer ;

Sqlite3不支持现有列的重命名,删除和修改。

 

插入数据:

insert into users(name) values('ruby');

_id作为自增主键,会从当前表中记录的最大下标开始增加;

也可以自己指定_id,但不能与现有记录的主键值冲突.

insert into users(_id,name) values (4,'lisi');

insert into users values (8,'zhangsan'); --users表中的所有字段(按照顺序)插入values后面的值.

查询数据:

select * from users;

-------------------------------------------------------------------------------------------------------------

创建表employees

create table employees (last_name VARCHAR2,  salary INTEGER, department_id INTEGER);

插入记录

insert into employees values("lisi",2000,1);

修改(更新)数据

语法格式:

UPDATE <表名>

SET <字段名> = value [, 字段名 = value, ...]

[WHERE  condition];

举例: update employees set salary = 4300 where salary = 4000;

删除记录

语法格式:

DELETE  FROM  <表名>

[WHERE   condition];

delete from employees where salary=1000;

基本查询

Select * from employees where salary>=2000;

查询的字段进行运算,起别名

select last_name,salary*12 年薪 from employees;

使用DISTINCT关键字可从查询结果中清除重复行

select distinct department_id from employees;

DISTINCT的作用范围是后面所有字段的组合:

SELECT DISTINCT department_id ,last_name

FROM   employees;

带有限制条件的查询

语法格式

SELECT [DISTINCT] {*, column [alias], ...}

FROM <表名>

[WHERE condition(s)];

使用比较运算符

不等于:  !=   <>

 

获取部门10的所有雇员的信息

SELECT *

FROM employees

WHERE department_id= 10;

 

使用BETWEEN   AND运算符显示某一 值域范围的记录

select * from employees where department_id between 1 and 2;

使用IN运算符获得匹配列表值的记录

select * from employees where department_id in(1,2);

使用LIKE运算符执行通配(模糊匹配)查询

查询条件可包含文字字符或数字

(%) 可表示零或多个字符

( _ ) 可表示一个字符

select * from employees where last_name like ‘%zhang_an’;  //代表以zhang_an结尾

                         ‘%k%’    //代表保护k的          ‘k%’  //代表以k开头的

使用IS NULL运算符

查询包含空值的记录

select * from employees where department_id IS NULL

IS NOT NULL

逻辑运算符

AND 如果组合的条件都是TRUE,返回TRUE

OR 如果组合的条件 之一是TRUE,返回TRUE

NOT 如果下面的条件是FALSE,返回TRUE

select * from doctor where salary>3000 and _id>1;

JAVA:   AND:  & &&

OR: |  ||

NOT: !

优先级规则

次序                          运算符

    1 所有的比较运算

2 NOT

3 AND

4 OR

对查询数据的排序

使用ORDER BY 子句将记录排序

ASC: 升序,(缺省的排序方式)

DESC: 降序

ORDER BY 子句出现在SELECT语句的最后

select * from employees order by department_id ;

select * from employees order by department_id desc;

按多字段排序

select * from employees order by department_id desc,salary asc;

 

1. 查询所有数据:
    select * from 表名;
    select * from exam_books;

    2.按照一定的条件查找:
    select * from 表名 where 条件;
    select * from exam_books where id<20;

    3.范围条件查询:
    select * from 表名 where 字段 between  值1  and 值2 ;
    select * from exam_books where id<20 and id>10;
    select * from exam_books where id between 10 and 20;
    select * from exam_books where addtime between '2011-03-17 00:00:00' and '2011-03-18 00:00:00';

    4.模糊查询:
    select * from 表名 where 字段 like '%条件%';
    select * from exam_books where bookname like '%马克思%';
    select * from exam_books where bookname like '马__';
    (查询书名中第一个字是“马”,后面有两个字)

    5.复合查询:
    select * from 表名 where 条件 and 另一个条件;
    select * from exam_questions  where coursecode = '03706' and chapterid = 2;

    6.查询个数:
    select count(*) from 表名 where 条件
    select count(*) as 别名 from exam_questions  where coursecode = '03706' and chapterid = 2;

    7.查询结果按顺序排列:
    正序、倒序(asc/desc)
    select * from 表名 where 条件 order by 字段 desc/asc;
    select * from exam_questions where coursecode = '03706' order by chapterid;

    8.按照limit查找某个范围内的数据:
    SELECT  * FROM exam_questions  order by id asc  limit  0, 15;(代表查找出的第一页的信息)
    SELECT  * FROM exam_questions  order by id asc  limit 15, 15;(代表查找出的第二页的信息)
    【备注:】limit后面的两个数字中:第一个代表偏移量,第二个代表每页展示的数量


    二. 删除数据:delete
    delete from 表名 where 条件;
    delete from exam_questions where id<2000;
    delete from exam_questions where coursecode='00041' and chapterid=1;


    三.插入新数据:insert
    insert into 表名(字段) values(值); 
    insert into exam_weburl(webname , weburl , info , bigtypeid) values('人人网', 'renren.com' , '这个网站不错' , 3);

    四.更新数据:update
    update 表名 set 字段1=值1, 字段2=值2 where 条件;
    update exam_weburl set info='这个网站不太好' where id=73;
    update exam_weburl set webname='人人2', weburl='www.renren.com' where id=73;


    五、创建表结构的语句:
    CREATE TABLE  表名 (_id INTEGER PRIMARY KEY AUTOINCREMENT , 字段2, 字段3...)
    例如:CREATE TABLE tb_newwords (_id INTEGER PRIMARY KEY AUTOINCREMENT , words  , detail );

    六、更新表结构的语句:
    1、如需在表中添加列,请使用下列语法:
    ALTER TABLE table_name
    ADD column_name datatype
    2、要删除表中的列,请使用下列语法:
    ALTER TABLE table_name 
    DROP COLUMN column_name
    3、要改变表中列的数据类型,请使用下列语法:
    ALTER TABLE table_name
    ALTER COLUMN column_name datatype
    【备注:】
    DDL (data define language)  数据定义语句 create,drop,alter
    DML (data manipulate language) 数据操纵语句 insert,delete,update,select
    DCL  数据控制语句 grant, revoke.
    TCL(transaction controll language)  事务控制语句. 
    学习SQL的主要重心应该放在DML语句上,而DML语句中的重点应该是在select语句上。
    CRUD:是指在做计算处理时的增加(Create)、查询(Retrieve)(重新得到数据)、更新(Update)和删除(Delete)几个单词的首字母简写。主要被用在描述数据库的基本操作。

3.2.sqlite数据库存储数据需要的数据类型  
  sqlite创建表时可以不指定数据类型 会根据第一条插入的数据识别数据类型
  字符串  varchar(10) nvarchar(10)
  字符    char(10) nchar(10)
  整型    integer
  浮点型   float double
  长文本   text

 

posted @ 2016-05-22 16:33  ts-android  阅读(568)  评论(0编辑  收藏  举报