加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLite学习笔记

发布时间:2020-12-12 23:47:29 所属栏目:百科 来源:网络整理
导读:http://weimingtom.iteye.com/blog/1515062 SQLite学习笔记 (未完成,待修改) 一、无库无表SQL 1. 无库连接sqlite3 (1) Windows提示符 sqlite3 SQLite version 3.6.22 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite

http://weimingtom.iteye.com/blog/1515062

SQLite学习笔记

(未完成,待修改)

一、无库无表SQL

1. 无库连接sqlite3

(1) Windows提示符

>sqlite3

SQLite version 3.6.22

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite>

(2) adb shell(模拟器或手机)

>adb shell

# sqlite3

sqlite3

2. 计算字段(通过运算符计算得到的字段,不存在于实际表中)

(1) 常量

sqlite> select 1;

1

(2) 四则运算

sqlite> select 3 + 4,3 - 4,3 * 4,3 / 4,3 % 4;

7|-1|12|0|3

sqlite> select 3 + 4 * 2;

11

sqlite> select (3 + 4) * 2;

14

sqlite> select -(1+0),+(2+0),~(2+0);

-1|2|-3

sqlite> select -("hello"),+("hello"),~("hello");

0|hello|-1

(3) 别名(导出列),相当于临时变量的赋值。

sqlite> select 3 * result,2 + result from (select 2 as result);

6|4

(4) 拼接(相当于MySQL的Concat())

sqlite> select 'hello' || ' ' || 'world!';

hello world!

sqlite> select 3 || 1;

31

sqlite> select LTrim(' hello') || RTrim(' world! ');

(5) 算术比较与逻辑与或运算

sqlite> select 3 > 2,3 < 2,3 = 3,3 == 3;

1|0|1|1

sqlite> select 1 != 0,1 <> 0;

1|1

sqlite> select 1 is null,1 is not null;

0|1

sqlite> select null is null,null is not null,null == null,null != null;

1|0||

sqlite> select (null == null) is null;

sqlite> select length(null) is null;

sqlite> select 1 = 1,1 == 1;

sqlite> select 3 between 4 and 5,4 between 4 and 5;

sqlite> select 4 in (1,3,4),0 in (1,2 not in (1,4);

1|0|1

sqlite> select (1 >= 2) and (1 <= 3),(1 >= 2) & (1 <= 3);

0|0

sqlite> select (1 < 2) or (1 > 3),(1 < 2) | (1 > 3);

sqlite> select 1 < 2,not (1 < 2);

1|0

sqlite> select case when 1 > 2 then 3 else 4 end;

4

sqlite> select case 2 when 1 then 3 else 4 end;

(6) 使用通配符的字符串匹配(MySQL没有glob。like通配符:%匹配0个或0个以上字符,_匹配1个字符,大小写不敏感;glob通配符:大小写敏感)

sqlite> select 'hello' like 'ell','hello' like 'ell%','hello' like '%ell%','hello' like 'he%','hello' like 'h%o';

0|0|1|1|1

sqlite> select 'hello' like '_ello','hello' like 'hel_';

sqlite> select 'Apple' like 'apple','Apple' like 'AppL_';

sqlite> select like('Apple','apple');

sqlite> select like('App%',sans-serif; font-size:14px; line-height:25.2000007629395px"> sqlite> select 'hello' glob 'he*','hello' glob 'hell?';

sqlite> select glob('he*','hello'),glob('hell?','hello');

(7) 使用正则表达式的字符串匹配(默认sqlite不实现REGEXP用户函数)

sqlite> select 'a' REGEXP '^a$';

Error: no such function: REGEXP

3. 使用数据处理函数的计算字段(通过函数计算得到的字段,不存在于实际表中)

(1) 文本处理(MySQL使用SubString,不支持MySQL的Soundex(),Left(),Right(),Locate())

sqlite> select str,Upper(str),Lower(str),Length(str) from (select 'Hello,World!' as str);

Hello,World!|HELLO,WORLD!|hello,world!|13

xxxHello,World!xxx|Hello,World!xxx|xxxHello,sans-serif; font-size:14px; line-height:25.2000007629395px"> sqlite> select soundex('Y. Lie');

Error: no such function: soundex

sqlite> select SubStr('Hello,World!',2,3);

ell

!

H

(2) 日期与时间处理(MySQL使用Now())

sqlite> select DateTime('now');

2012-05-07 05:34:36

sqlite> select strftime('%s','now');

1336369355

(3) 数值处理(MySQL使用Rand(),不支持MySQL的Sin(),Cos(),Tan(),Exp(),Mod(),Pi(),Sqrt())

sqlite> select Abs(-1.2);

1.2

sqlite> select random();

1872328235102936735

(4) 聚集函数(只有Min()和Max()可用于不同列,AVG(),COUNT(),SUM()则不可以用在这里)

sqlite> select min(5,6,7),max(5,7);

5|7

(5) like()与glob()的字符串匹配

见前,略。

(TODO:)

X:其它:

(8) 全文搜索和MATCH(MySQL则需要用FULLTEXT()在CREATE TABLE中指定列,用Match()指定被搜索列,用Against()指定表达式)

>sqlite3 test2.sqlite

sqlite> .tables

sqlite> CREATE VIRTUAL TABLE mail USING fts3(subject,body);

mail mail_content mail_segdir mail_segments

sqlite> INSERT INTO mail(docid,subject,body) VALUES(1,'software feedback','found it too slow');

sqlite> SELECT * FROM mail WHERE subject MATCH 'software';

software feedback|found it too slow

software feedback|no feedback

----------------------------- 需要注意的问题: * 命令行命令可以缩略,如.sc等效于.schema * 命令行命令千万不能以分号结束,如.schema words不能写成.schema words;,得到的结果是不同的 * 主键约束(主键primary key必须是唯一的)可以写在列中,也可以单独写在一个列的位置: CREATE TABLE t3(id integer primary key,a integer); CREATE TABLE t2(id integer,a integer,primary key(id) ); 但对于自增长的主键,只能用前者的语法: create table t5(id integer primary key autoincrement,sans-serif; font-size:14px; line-height:25.2000007629395px"> * 全文搜索的ft3表使用primary key autoincrement是不起作用的(可以在CREATE TABLE中指定,但插入数据后总为null)。可考虑用rowid或docid代替。 (20140903) Android 4collation问题 Android 4支持一种特殊的排序方式COLLATE LOCALIZED,这种方式在低版本的Android和sqlite官方版(JDBC驱动)会报错(可能是因为需要加载某个特定的扩展)。 CREATE TABLE test (id INTEGER PRIMARY KEY,data TEXT COLLATE LOCALIZED); SELECT data FROM test ORDER BY data COLLATE LOCALIZED ASC 其他高级用法: 请参考tutorialspoint的教程: http://www.tutorialspoint.com/sqlite/index.htm ------------------------------ 参考链接收集 1.2010年SQLite学习笔记之一 http://www.52php.cn/article/p-qiudiwhw-co.html (20131211) 2. sqlite查询优化 http://stackoverflow.com/questions/12831504/quick-readonly-sqlite-database http://web.utk.edu/~jplyon/sqlite/SQLite_optimization_FAQ.html (20140704) 3.Sqlite语法 http://www.cnblogs.com/helloandroid/articles/2150272.html 4. Sqlite基础教程与高级用法 http://www.tutorialspoint.com/sqlite/index.htm ------------------------------------------ 使用技巧摘录(可能只适用于jdbc版不适用于Android版) (20141211)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读