设为首页 收藏本站
查看: 2221|回复: 0

[经验分享] SQLite学习笔记

[复制链接]

尚未签到

发表于 2016-11-28 11:17:59 | 显示全部楼层 |阅读模式
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
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
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! ');
hello 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;
1
sqlite> select length(null) is null;
1
sqlite> select 1 = 1, 1 == 1;
1|1
sqlite> select 3 between 4 and 5, 4 between 4 and 5;
0|1
sqlite> select 4 in (1, 3, 4), 0 in (1, 3, 4), 2 not in (1, 3, 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);
1|1
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;
4
 
(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_';
1|0
sqlite> select 'Apple' like 'apple', 'Apple' like 'AppL_';
1|1
sqlite> select like('Apple', 'apple');
1
sqlite> select like('App%', 'apple');
1
sqlite> select 'hello' glob 'he*', 'hello' glob 'hell?';
1|1
sqlite> select glob('he*', 'hello'), glob('hell?', 'hello');
1|1
(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
sqlite> select str, LTrim(str), RTrim(str), Trim(str) from (select '  Hello, World!  ' as str);
Hello, World!  |Hello, World!  |  Hello, World!|Hello, World!
sqlite> select str, LTrim(str, 'x'), RTrim(str, 'x'), Trim(str, 'x') from (select 'xxxHello, World!xxx' as str);
xxxHello, World!xxx|Hello, World!xxx|xxxHello, World!|Hello, World!
sqlite> select soundex('Y. Lie');
Error: no such function: soundex
sqlite> select SubStr('Hello, World!', 2, 3);
ell
sqlite> select SubStr('Hello, World!', -1, 4);
!
sqlite> select SubStr('Hello, World!', 2, -2);
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, 6, 7);
5|7
(5) like()与glob()的字符串匹配
见前,略。
 
 
 
(TODO:)
 
 
 
X:其它:
(8) 全文搜索和MATCH(MySQL则需要用FULLTEXT()在CREATE TABLE中指定列,用Match()指定被搜索列,用Against()指定表达式)
>sqlite3 test2.sqlite
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
sqlite> CREATE VIRTUAL TABLE mail USING fts3(subject, body);
sqlite> .tables
mail           mail_content   mail_segdir    mail_segments
sqlite> INSERT INTO mail(docid, subject, body) VALUES(1, 'software feedback', 'found it too slow');
sqlite> INSERT INTO mail(docid, subject, body) VALUES(2, 'software feedback', 'no feedback');
sqlite> INSERT INTO mail(docid, subject, body) VALUES(3, 'slow lunch order',  'was a software problem');
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, a integer);

* 全文搜索的ft3表使用primary key autoincrement是不起作用的(可以在CREATE TABLE中指定,但插入数据后总为null)。可考虑用rowid或docid代替。

 

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

(20140903)

Android 4 collation问题

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://blog.csdn.net/littletigerat/article/details/5312875

(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)

一、 调节数据库缓存大小

private void setPragmaCacheSize(Connection connection) {
Statement s = null;
try {
s = connection.createStatement();
s.execute("PRAGMA cache_size = " + DB_CACHE_SIZE + ";");
} catch (SQLException e) {
LogUtil.error(logger, "PRAGMA cache_size error", e);
} finally {
try {
if (s != null) {
s.close();
}
} catch (SQLException e2) {
e2.printStackTrace();
}
}
}
使用:

con = DriverManager.getConnection(url);
setPragmaCacheSize(con);
  

参考自:

* jawjaw

https://code.google.com/p/jawjaw/

 

 

 

 

(TODO:)
 
 
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306665-1-1.html 上篇帖子: SQLite资料翻译 下篇帖子: sqlite 客户端
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表