首页 > SQL > SQLite学习笔记

SQLite学习笔记

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

  1. Maxine 1月 17th, 2013 @ 12:33 | #1

    This is usually a really great site content, im delighted I came across it. Ill be back off the track to look at other reports that.

评论提交中, 请稍候...

留言


可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks