美文网首页
sqlite基础

sqlite基础

作者: ScratchPad | 来源:发表于2018-12-23 20:55 被阅读0次

概况

SQLite独有的功能

Zero-Configuration : 配置简单

Serverless:基于文件的,没有类似于SQLServer之类的服务端

SingleDatabase File:单个数据库文件

Stable Cross-Platform Database File: 因为简单,因此跨平台兼容性好

Compact: 整个SQLITE库文件大小很小,运行起来所需的资源也很少

Manifest typing:为了实现简单,没有使用像其他数据库引擎使用的静态类型机制,二是采用了存储类型机制,只有NULL/INTEGER/REAL/TEXT/BLOB这五种存储类型(storage class),存储类型比数据类型更泛泛。
https://sqlite.org/datatype3.html

Variable-length records: 记录长度是可变的,而许多的其他的数据库实现都给每行分配一个固定长度的空间。

Readable source code:源代码开源

SQL statements compile into virtual machine code:执行优化

Public domain:没有任何授权上的限制,但是文档和扩展不包括在内

SQL language extensions : 提供了灵活的扩展机制

SQLite没有实现的功能

RIGHT and FULL [OUTER] JOIN : 个人猜测应该是为了方便实现,只实现了LEFT JOININNER JOIN,但是可以通过交换表名用LEFT JOIN来实现RIGHT JOIN,而FULL [OUTER] JOIN可以利用 ((LEFT JOIN) UNION (RIGHT JOIN))来实现。这里补充一下,FULL OUTER JOIN有时会简称为FULL JOIN

Complete ALTER TABLE support : 对表的操作,只支持对表名进行重命名和添加数据列,不支持drop table, alter column, 以及add constraint,统统只能通过删除-新建的方式来达成想要的改变。

Complete trigger Support : SQLITE没有实现完整的触发器机制,只支持行级触发器,而不支持语句级的触发器,也就是粒度不够。

Writing to VIEWS : SQLITE中的视图是只读的,你不能在VIEW中执行一个DELETE/INSERT/UPDATE操作

GRANT and REVOKE:sqlite只是内存+磁盘文件,没有提供除了底层系统所提供的权限控制机制以外的任何权限控制机制。

Storage Procedure: 不支持存储过程,至于存储过程能否提升数据库访问的性能,It's debatable,看具体的实现。

PRAGMA声明

PRAGMA声明是一个SQL扩展,专门用来修改SQLITE库的操作或者查询一些内部数据(非表数据)。和其他SQLITE命令一样,PRAGMA声明也是用类似SELECT、INSERT关键字来发出的,但是又有以下的不同:

  1. 某些PRAGMA声明可能会在将来的版本移除,PRAGMA机制并不保证后向兼容性
  2. 如果使用未知的PRAGMA声明,并不会打印错误信息。未知的PRAGMA直接被忽略掉。
  3. 有些PRAGMA在sql语句的编译阶段起作用,而不是执行阶段。这意味着如果使用sqlite3_prepare()、sqlite3_step()、sqlite3_finalize()接口的话,PRAGMA可能在sqlite3_prepare()调用的时候运行,而不是和常规的sql语句一样在sqlite3_step()调用的时候运行。
  4. PRAGMA命令只对sqlite起作用,当然这并不意味着其他数据库引擎(这里需要对DBMS的架构有所了解,数据库引擎(或存储引擎)是数据库管理系统(DBMS)用于从数据库创建,读取,更新和删除(CRUD)数据的底层软件组件)不支持PRAGMA,又或者其他实现不和这里描述的PRAGMA没有共同之处,只是说PRAGMA不是标准的一部分,也就是PRAGMA的实现会因数据库引擎不同而不同。

PRAGMA命令语法

pragma-stmt ::= PRAGMA [schema-name.]pragma-name[=pragma-value|(pragma-value)]

pragma-value ::= signed-number|name|string-literal

signed-number ::= [+|-]numeric-literal

其中schema-name是当前attached(这个概念也是针对sqlite的,但不一定是独有的)的数据库,通常有maintemp,如果省略schema-name,那么系统会假定采用main。有的PRAGMA语句在schema-name没有任何实际意义的时候会忽略掉schema-name,注意这是忽略而不是省略。

PRAGMA 函数

返回结果并且没有副作用的PRAGMA可以从普通的SELECT语句中作为表值函数(table-valued functions)来访问。而对应的函数名称则是前缀pragma_加上pragma-name,而pragma参数和schema,如果存在的话则当做表值函数的参数传递。

这样的好处是,可以使用SELECT语句的where语句,可以使用并集函数,并且可以使用别的表值函数作为输入。

额外需要注意的是:

  1. 表值函数只为了内置的PRAGMA存在,使用SQLITE_FCNTL_PRAGMA文件控制定义的PRAGMA则没有对应的表值函数

  2. 只有能返回结果并且无副作用的PRAGMA有对应的表值函数

  3. This feature could be used to implement information schema by first creating a separate schema using

ATTACH ':memory:' AS 'information_schema';
Then creating VIEWs in that schema that implement the official information schema tables using table-valued PRAGMA functions.

  1. 这个功能是实验性的,可能会在之后的版本发生改变

  2. 这个功能是在3.16.0后才添加的

函数

核心函数

函数名 描述
abs(X) 该函数返回数值参数X的绝对值,如果X为NULL,则返回NULL,如果X为不能转换成数值的字符串,则返回0,如果X值是Integer类型,并且转换后超出Integer的上限,则抛出"Integer Overflow"的异常。
changes() 该函数返回最近执行的INSERT、UPDATE或DELETE语句所影响的数据行数。我们也可以通过执行C/C++函数sqlite3_changes()得到相同的结果。
char(X1,X2,...,XN) SELECT char(67,72,65,82); > CHAR,其实就是把X1、X2...XN对应的ascii码拼成一个字符串,这要求所有的参数都是integer
coalesce(X,Y,...) 返回函数参数中第一个非NULL的参数,如果参数都是NULL,则返回NULL。该函数至少2个参数。
glob(x,y) The glob(X,Y) function is equivalent to the expression "Y GLOB X". Note that the X and Y arguments are reversed in the glob() function relative to the infix GLOB operator. If the sqlite3_create_function() interface is used to override the glob(X,Y) function with an alternative implementation then the GLOB operator will invoke the alternative implementation.
hex(X) The hex() function interprets its argument as a BLOB and returns a string which is the upper-case hexadecimal rendering of the content of that blob.
ifnull(X,Y) 该函数等同于两个参数的coalesce()函数,即返回第一个不为NULL的函数参数,如果两个均为NULL,则返回NULL。
instr(X,Y) instr(X,Y)函数查找字符串X中字符串Y的第一次出现,并返回这个位置前的字符的数目加1,或者如果Y在X中没有找到,则返回0。或者,如果X和Y都是BLOB类型,则instr(X,Y)则是根据字节来实现前面的操作。如果两个参数X和Y都是非NULL并且不是BLOB类型,那么X和Y则被当成字符类型。如果instr(X,Y)中的X或Y是NULL,则结果为NULL。
last_insert_rowid() 函数返回调用这个函数之前最后一条插入的行的ROWID,last_insert_rowid()函数其实是封装了sqlite3_last_insert_rowid()
length(X) 如果参数X为字符串,则返回字符的数量,如果为数值,则返回该参数的字符串表示形式的长度,如果X为NULL,则返回NULL。
like(X,Y[,z]) 这个函数用来实现'Y LIKE X [ESCAPE Z]'表达式,因此可以通过使用sqlite3_create_function()函数来覆写like()函数来改变LIKE操作符的行为
likelihood(X,Y) Y只能在0.0和1.0的闭区间之间取值。The purpose of the likelihood(X,Y) function is to provide a hint to the query planner that the argument X is a boolean that is true with a probability of approximately Y
likely(X) likelihood(X,0.9375)的速记写法
load_extension(X[,Y]) 加载扩展,Y为扩展的入口点
lower(X) 返回函数参数X的小写形式,缺省情况下,该函数只能应用于ASCII字符。
ltrim(X[,Y]) 如果没有可选参数Y,该函数将移除参数X左侧的所有空格符。如果有参数Y,则移除X左侧的任意在Y中出现的字符。最后返回移除后的字符串。
max(X,Y,...) 返回函数参数中的最大值,如果有任何一个参数为NULL,则返回NULL。
min(X,Y,...) 返回函数参数中的最小值,如果有任何一个参数为NULL,则返回NULL。
nullif(X,Y) 如果函数参数相同,返回NULL,否则返回第一个参数。
printf(FORMAT,...) The printf(FORMAT,...) SQL function works like the sqlite3_mprintf() C-language function and the printf() function from the standard C library. The first argument is a format string that specifies how to construct the output string using values taken from subsequent arguments. If the FORMAT argument is missing or NULL then the result is NULL. The %n format is silently ignored and does not consume an argument. The %p format is an alias for %X. The %z format is interchangeable with %s. If there are too few arguments in the argument list, missing arguments are assumed to have a NULL value, which is translated into 0 or 0.0 for numeric formats or an empty string for %s.
quote(X) The quote(X) function returns the text of an SQL literal which is the value of its argument suitable for inclusion into an SQL statement. Strings are surrounded by single-quotes with escapes on interior quotes as needed. BLOBs are encoded as hexadecimal literals. Strings with embedded NUL characters cannot be represented as string literals in SQL and hence the returned string literal is truncated prior to the first NUL.
random() 返回整型的伪随机数。
randomblob(N) 返回N个任意的字节,如果N小于1,则范围一个长度为1的字节
replace(X,Y,Z) 将字符串类型的函数参数X中所有子字符串Y替换为字符串Z,最后返回替换后的字符串,源字符串X保持不变。
round(X[,Y]) 返回数值参数X被四舍五入到Y刻度的值,如果参数Y不存在,缺省参数值为0。
rtrim(X[,Y]) 如果没有可选参数Y,该函数将移除参数X右侧的所有空格符。如果有参数Y,则移除X右侧的任意在Y中出现的字符。最后返回移除后的字符串。
soundex(X) The soundex(X) function returns a string that is the soundex encoding of the string X. The string "?000" is returned if the argument is NULL or contains no ASCII alphabetic characters. This function is omitted from SQLite by default. It is only available if the SQLITE_SOUNDEX compile-time option is used when SQLite is built.
sqlite_compileoption_get(N) The sqlite_compileoption_get() SQL function is a wrapper around the sqlite3_compileoption_get() C/C++ function. This routine returns the N-th compile-time option used to build SQLite or NULL if N is out of range. See also the compile_options pragma.
sqlite_compileoption_used(X) The sqlite_compileoption_used() SQL function is a wrapper around the sqlite3_compileoption_used() C/C++ function. When the argument X to sqlite_compileoption_used(X) is a string which is the name of a compile-time option, this routine returns true (1) or false (0) depending on whether or not that option was used during the build.
sqlite_source_id() The sqlite_source_id() function returns a string that identifies the specific version of the source code that was used to build the SQLite library. The string returned by sqlite_source_id() is the date and time that the source code was checked in followed by the SHA1 hash for that check-in. This function is an SQL wrapper around the sqlite3_sourceid() C interface.
sqlite_version() The sqlite_version() function returns the version string for the SQLite library that is running. This function is an SQL wrapper around the sqlite3_libversion() C-interface.
substr(X,Y[,Z]) 返回函数参数X的子字符串,从第Y位开始(X中的第一个字符位置为1)截取Z长度的字符,如果忽略Z参数,则取第Y个字符后面的所有字符。如果Z的值为负数,则从第Y位开始,向左截取abs(Z)个字符。如果Y值为负数,则从X字符串的尾部开始计数到第abs(Y)的位置开始。
total_changes() 该函数返回自从该连接被打开时起,INSERT、UPDATE和DELETE语句总共影响的行数。我们也可以通过C/C++接口函数sqlite3_total_changes()得到相同的结果。
trim(x[,y]) 如果没有可选参数Y,该函数将移除参数X两侧的所有空格符。如果有参数Y,则移除X两侧的任意在Y中出现的字符。最后返回移除后的字符串。
upper(X) 返回函数参数X的大写形式,缺省情况下,该函数只能应用于ASCII字符。
typeof(X) 返回函数参数数据类型的字符串表示形式,如"Integer、text、real、null"等。
unicode(X) The unicode(X) function returns the numeric unicode code point corresponding to the first character of the string X. If the argument to unicode(X) is not a string then the result is undefined.
unlikely(X) likelihood(X, 0.0625)的速记写法(short-hand)
upper(X) The upper(X) function returns a copy of input string X in which all lower-case ASCII characters are converted to their upper-case equivalent.
zeroblob(N) The zeroblob(N) function returns a BLOB consisting of N bytes of 0x00. SQLite manages these zeroblobs very efficiently. Zeroblobs can be used to reserve space for a BLOB that is later written using incremental BLOB I/O. This SQL function is implemented using the sqlite3_result_zeroblob() routine from the C/C++ interface.

并集函数

函数名 描述
avg(x) 该函数返回在同一组内参数字段的平均值。对于不能转换为数字值的String和BLOB类型的字段值,如'HELLO',SQLite会将其视为0。avg函数的结果总是浮点型,唯一的例外是所有的字段值均为NULL,那样该函数的结果也为NULL。
count(x|*) count(x)函数返回在同一组内,x字段中值不等于NULL的行数。count(*)函数返回在同一组内的数据行数。
group_concat(x[,y]) 该函数返回一个字符串,该字符串将会连接所有非NULL的x值。该函数的y参数将作为每个x值之间的分隔符,如果在调用时忽略该参数,在连接时将使用缺省分隔符","。再有就是各个字符串之间的连接顺序是不确定的。
max(x) 该函数返回同一组内的x字段的最大值,如果该字段的所有值均为NULL,该函数也返回NULL。
min(x) 该函数返回同一组内的x字段的最小值,如果该字段的所有值均为NULL,该函数也返回NULL。
sum(x) 该函数返回同一组内的x字段值的总和,如果字段值均为NULL,该函数也返回NULL。如果所有的x字段值均为整型或者NULL,该函数返回整型值,否则就 返回浮点型数值。最后需要指出的是,如果所有的数据值均为整型,一旦结果超过上限时将会抛出"integer overflow"的异常。
total(x) 该函数不属于标准SQL,其功能和sum基本相同,只是计算结果比sum更为合理。比如当所有字段值均为NULL时,和sum不同的是,该函数返回0.0。再有就是该函数始终返回浮点型数值。该函数始终都不会抛出异常。

时间和日期函数

时间这部分因为细节比较多,就不详细翻译了。

sqlite支持以下五种时间日期函数

date(timestring, modifier, modifier, ...)
time(timestring, modifier, modifier, ...)
datetime(timestring, modifier, modifier, ...)
julianday(timestring, modifier, modifier, ...)
strftime(format, timestring, modifier, modifier, ...)

Android相关的细节

可以在/path/to/aosp/external/sqlite/中查看sqlite的源码

根据/path/to/aosp/external/sqlite/dist/Android.bp中的cflags可以得知:

LOAD_EXTENSION()函数
PRAGMA COMPILE_OPTIONS

都默认被禁用了,所以如果需要使用这些功能,可能需要在应用内置自己编译的sqlite。

参考

creating-stored-procedure-and-sqlite
sqlite changelog
Distinctive Features of SQLite
Date and Time functions
char()

相关文章

  • SQLite3深入浅出

    文章目录: sqlite3 基础语句 sqlite3 API sqlite3 线程安全 FMDB 基础语句: 创建...

  • 数据库 - sqlite3 基础 - 结合 fmdb

    http://www.runoob.com/sqlite/sqlite-tutorial.htmlsqlite基础...

  • SQLite的基础知识

    数据库SQLite的基础语法 SQLite 1. 什么是SQLite: (1) SQLite是一款轻型的嵌入式数据...

  • Android数据库代码优化(2) - 从SQLite说起

    从SQLite说起 如果没有SQLite的基础,我们只是从Android封装的SQLite API去学习的话,难免...

  • SQLite基础

    在移动端中保持数据持久化有: plist(NSArray、NSDictionary) 只能存储数据 Prefere...

  • SQLite基础

    1.在Android系统中,除了可以使用文件或者SharedPreference存储数据,还可以选择使用SQLit...

  • SQLite 基础

    一、iOS中的数据存储方式 Plist(NSArray\NSDictionary) Preference(偏好设置...

  • sqlite基础

    SQLite 什么是SQLite SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在...

  • sqlite基础

    概况 SQLite独有的功能 Zero-Configuration : 配置简单 Serverless:基于文件的...

  • SQLite

    SQLite 基础知识 SQLite 列操作 sqlite中不支持删除列的操作,也不能够修改主键。 从上图可见,S...

网友评论

      本文标题:sqlite基础

      本文链接:https://www.haomeiwen.com/subject/iqcokqtx.html