SQLite 运算符是什么? 运算符是一个保留字或字符,主要用于 SQLite 语句的 WHERE 子句中执行操作,如比较和算术运算。 运算符用于指定 SQLite 语句中的条件,并在语句中连接多个条件。 算术运算符 比较运算符 逻辑运算符 位运算符 SQLite 算术运算符 假设变量 a=10,变量 b=20,则: 运算符 描述 实例 + 加法 – 把运算符两边的值相加 a + b 将得到 30 – 减法 – 左操作数减去右操作数 a – b 将得到 -10 * 乘法 – 把运算符两边的值相乘 a * b 将得到 200 / 除法 – 左操作数除以右操作数 b / a 将得到 2 % 取模 – 左操作数除以右操作数后得到的余数 b % a will give 0 实例 下面是 SQLite 算术运算符的简单实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span class="pln">sqlite</span><span class="pun">></span> <span class="pun">.</span><span class="pln">mode line sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="lit">10</span> <span class="pun">+</span> <span class="lit">20</span><span class="pun">;</span> <span class="lit">10</span> <span class="pun">+</span> <span class="lit">20</span> <span class="pun">=</span> <span class="lit">30</span><span class="pln"> sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="lit">10</span> <span class="pun">-</span> <span class="lit">20</span><span class="pun">;</span> <span class="lit">10</span> <span class="pun">-</span> <span class="lit">20</span> <span class="pun">=</span> <span class="pun">-</span><span class="lit">10</span><span class="pln"> sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="lit">10</span> <span class="pun">*</span> <span class="lit">20</span><span class="pun">;</span> <span class="lit">10</span> <span class="pun">*</span> <span class="lit">20</span> <span class="pun">=</span> <span class="lit">200</span><span class="pln"> sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="lit">10</span> <span class="pun">/</span> <span class="lit">5</span><span class="pun">;</span> <span class="lit">10</span> <span class="pun">/</span> <span class="lit">5</span> <span class="pun">=</span> <span class="lit">2</span><span class="pln"> sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="lit">12</span> <span class="pun">%</span> <span class="lit">5</span><span class="pun">;</span> <span class="lit">12</span> <span class="pun">%</span> <span class="lit">5</span> <span class="pun">=</span> <span class="lit">2</span> |
SQLite 比较运算符 假设变量 a=10,变量 b=20,则: 运算符 描述 实例 == 检查两个操作数的值是否相等,如果相等则条件为真。 (a == b) 不为真。 = […]
View DetailsSQLite 的 SELECT 语句用于从 SQLite 数据库表中获取数据,以结果表的形式返回数据。这些结果表也被称为结果集。 语法 SQLite 的 SELECT 语句的基本语法如下:
1 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> columnN FROM table_name</span><span class="pun">;</span> |
在这里,column1, column2…是表的字段,他们的值即是您要获取的。如果您想获取所有可用的字段,那么可以使用下面的语法:
1 |
<span class="pln">SELECT </span><span class="pun">*</span><span class="pln"> FROM table_name</span><span class="pun">;</span> |
实例 假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
下面是一个实例,使用 SELECT 语句获取并显示所有这些记录。在这里,前三个命令被用来设置正确格式化的输出。
1 2 3 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">header on sqlite</span><span class="pun">>.</span><span class="pln">mode column sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY</span><span class="pun">;</span> |
最后,将得到以下的结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
如果只想获取 COMPANY 表中指定的字段,则使用下面的查询:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT ID</span><span class="pun">,</span><span class="pln"> NAME</span><span class="pun">,</span><span class="pln"> SALARY FROM COMPANY</span><span class="pun">;</span> |
上面的查询会产生以下结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">10000.0</span> |
设置输出列的宽度 有时,由于要显示的列的默认宽度导致 .mode column,这种情况下,输出被截断。此时,您可以使用 .width num, num…. 命令设置显示列的宽度,如下所示:
1 2 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">width </span><span class="lit">10</span><span class="pun">,</span> <span class="lit">20</span><span class="pun">,</span> <span class="lit">10</span><span class="pln"> sqlite</span><span class="pun">></span><span class="pln">SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY</span><span class="pun">;</span> |
上面的 .width 命令设置第一列的宽度为 10,第二列的宽度为 20,第三列的宽度为 10。因此上述 SELECT 语句将得到以下结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">--------------------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
Schema 信息 因为所有的点命令只在 SQLite 提示符中可用,所以当您进行带有 SQLite 的编程时,您要使用下面的带有 sqlite_master 表的 SELECT 语句来列出所有在数据库中创建的表:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT tbl_name FROM sqlite_master WHERE type </span><span class="pun">=</span> <span class="str">'table'</span><span class="pun">;</span> |
假设在 testDB.db 中已经存在唯一的 COMPANY 表,则将产生以下结果:
1 2 3 |
<span class="pln">tbl_name </span><span class="pun">----------</span><span class="pln"> COMPANY</span> |
您可以列出关于 COMPANY 表的完整信息,如下所示:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT sql FROM sqlite_master WHERE type </span><span class="pun">=</span> <span class="str">'table'</span><span class="pln"> AND tbl_name </span><span class="pun">=</span> <span class="str">'COMPANY'</span><span class="pun">;</span> |
假设在 testDB.db 中已经存在唯一的 COMPANY 表,则将产生以下结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">CREATE TABLE COMPANY</span><span class="pun">(</span><span class="pln"> ID INT PRIMARY KEY NOT NULL</span><span class="pun">,</span><span class="pln"> NAME TEXT NOT NULL</span><span class="pun">,</span><span class="pln"> AGE INT NOT NULL</span><span class="pun">,</span><span class="pln"> ADDRESS CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">),</span><span class="pln"> SALARY REAL </span><span class="pun">) from:<a href="http://www.runoob.com/sqlite/sqlite-select.html">http://www.runoob.com/sqlite/sqlite-select.html</a></span> |
SQLite 的 INSERT INTO 语句用于向数据库的某个表中添加新的数据行。 语法 INSERT INTO 语句有两种基本语法,如下所示:
1 2 |
<span class="pln">INSERT INTO TABLE_NAME </span><span class="pun">(</span><span class="pln">column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> column3</span><span class="pun">,...</span><span class="pln">columnN</span><span class="pun">)]</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="pln">value1</span><span class="pun">,</span><span class="pln"> value2</span><span class="pun">,</span><span class="pln"> value3</span><span class="pun">,...</span><span class="pln">valueN</span><span class="pun">);</span> |
在这里,column1, column2,…columnN 是要插入数据的表中的列的名称。 如果要为表中的所有列添加值,您也可以不需要在 SQLite 查询中指定列名称。但要确保值的顺序与列在表中的顺序一致。SQLite 的 INSERT INTO 语法如下:
1 |
<span class="pln">INSERT INTO TABLE_NAME VALUES </span><span class="pun">(</span><span class="pln">value1</span><span class="pun">,</span><span class="pln">value2</span><span class="pun">,</span><span class="pln">value3</span><span class="pun">,...</span><span class="pln">valueN</span><span class="pun">);</span> |
实例 假设您已经在 testDB.db 中创建了 COMPANY表,如下所示:
1 2 3 4 5 6 7 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> CREATE TABLE COMPANY</span><span class="pun">(</span><span class="pln"> ID INT PRIMARY KEY NOT NULL</span><span class="pun">,</span><span class="pln"> NAME TEXT NOT NULL</span><span class="pun">,</span><span class="pln"> AGE INT NOT NULL</span><span class="pun">,</span><span class="pln"> ADDRESS CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">),</span><span class="pln"> SALARY REAL </span><span class="pun">);</span> |
现在,下面的语句将在 COMPANY 表中创建六个记录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="pln">INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span> <span class="str">'Paul'</span><span class="pun">,</span> <span class="lit">32</span><span class="pun">,</span> <span class="str">'California'</span><span class="pun">,</span> <span class="lit">20000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span> <span class="str">'Allen'</span><span class="pun">,</span> <span class="lit">25</span><span class="pun">,</span> <span class="str">'Texas'</span><span class="pun">,</span> <span class="lit">15000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span> <span class="str">'Teddy'</span><span class="pun">,</span> <span class="lit">23</span><span class="pun">,</span> <span class="str">'Norway'</span><span class="pun">,</span> <span class="lit">20000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">4</span><span class="pun">,</span> <span class="str">'Mark'</span><span class="pun">,</span> <span class="lit">25</span><span class="pun">,</span> <span class="str">'Rich-Mond '</span><span class="pun">,</span> <span class="lit">65000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">5</span><span class="pun">,</span> <span class="str">'David'</span><span class="pun">,</span> <span class="lit">27</span><span class="pun">,</span> <span class="str">'Texas'</span><span class="pun">,</span> <span class="lit">85000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">6</span><span class="pun">,</span> <span class="str">'Kim'</span><span class="pun">,</span> <span class="lit">22</span><span class="pun">,</span> <span class="str">'South-Hall'</span><span class="pun">,</span> <span class="lit">45000.00</span> <span class="pun">);</span> |
您也可以使用第二种语法在 COMPANY 表中创建一个记录,如下所示:
1 |
<span class="pln">INSERT INTO COMPANY VALUES </span><span class="pun">(</span><span class="lit">7</span><span class="pun">,</span> <span class="str">'James'</span><span class="pun">,</span> <span class="lit">24</span><span class="pun">,</span> <span class="str">'Houston'</span><span class="pun">,</span> <span class="lit">10000.00</span> <span class="pun">);</span> |
上面的所有语句将在 COMPANY 表中创建下列记录。下一章会教您如何从一个表中显示所有这些记录。
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
使用一个表来填充另一个表 您可以通过在一个有一组字段的表上使用 select 语句,填充数据到另一个表中。下面是语法:
1 2 3 4 |
<span class="pln">INSERT INTO first_table_name </span><span class="pun">[(</span><span class="pln">column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span> <span class="pun">...</span><span class="pln"> columnN</span><span class="pun">)]</span><span class="pln"> SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span> <span class="pun">...</span><span class="pln">columnN FROM second_table_name </span><span class="pun">[</span><span class="pln">WHERE condition</span><span class="pun">];</span> |
您暂时可以先跳过上面的语句,可以先学习后面章节中介绍的 SELECT 和 WHERE 子句。 from:http://www.runoob.com/sqlite/sqlite-insert.html
View DetailsSQLite 的 DROP TABLE 语句用来删除表定义及其所有相关数据、索引、触发器、约束和该表的权限规范。 使用此命令时要特别注意,因为一旦一个表被删除,表中所有信息也将永远丢失。 语法 DROP TABLE 语句的基本语法如下。您可以选择指定带有表名的数据库名称,如下所示:
1 |
<span class="pln">DROP TABLE database_name</span><span class="pun">.</span><span class="pln">table_name</span><span class="pun">;</span> |
实例 让我们先确认 COMPANY 表已经存在,然后我们将其从数据库中删除。
1 2 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">tables COMPANY test</span><span class="pun">.</span><span class="pln">COMPANY</span> |
这意味着 COMPANY 表已存在数据库中,接下来让我们把它从数据库中删除,如下:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln">DROP TABLE COMPANY</span><span class="pun">;</span><span class="pln"> sqlite</span><span class="pun">></span> |
现在,如果尝试 .TABLES 命令,那么将无法找到 COMPANY 表了:
1 2 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">tables sqlite</span><span class="pun">></span> |
显示结果为空,意味着已经成功从数据库删除表。 from:http://www.runoob.com/sqlite/sqlite-drop-table.html
View DetailsSQLite 的 CREATE TABLE 语句用于在任何给定的数据库创建一个新表。创建基本表,涉及到命名表、定义列及每一列的数据类型。 语法 CREATE TABLE 语句的基本语法如下:
1 2 3 4 5 6 7 |
CREATE TABLE database_name.table_name( column1 datatype PRIMARY KEY(one or more columns), column2 datatype, column3 datatype, ..... columnN datatype, ); |
CREATE TABLE 是告诉数据库系统创建一个新表的关键字。CREATE TABLE 语句后跟着表的唯一的名称或标识。您也可以选择指定带有 table_name 的 database_name。 实例 下面是一个实例,它创建了一个 COMPANY 表,ID 作为主键,NOT NULL 的约束表示在表中创建纪录时这些字段不能为 NULL:
1 2 3 4 5 6 7 |
sqlite> CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL ); |
让我们再创建一个表,我们将在随后章节的练习中使用:
1 2 3 4 5 |
sqlite> CREATE TABLE DEPARTMENT( ID INT PRIMARY KEY NOT NULL, DEPT CHAR(50) NOT NULL, EMP_ID INT NOT NULL ); |
您可以使用 SQLIte 命令中的 .tables 命令来验证表是否已成功创建,该命令用于列出附加数据库中的所有表。
1 2 |
sqlite>.tables COMPANY DEPARTMENT |
在这里,可以看到我们刚创建的两张表 COMPANY、 DEPARTMENT。 您可以使用 SQLite .schema 命令得到表的完整信息,如下所示:
1 2 3 4 5 6 7 8 |
sqlite>.schema COMPANY CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL ); |
from:http://www.runoob.com/sqlite/sqlite-create-table.html
View DetailsSQLite的 DETACH DTABASE 语句是用来把命名数据库从一个数据库连接分离和游离出来,连接是之前使用 ATTACH 语句附加的。如果同一个数据库文件已经被附加上多个别名,DETACH 命令将只断开给定名称的连接,而其余的仍然有效。您无法分离 main 或 temp 数据库。 如果数据库是在内存中或者是临时数据库,则该数据库将被摧毁,且内容将会丢失。 语法 SQLite 的 DETACH DATABASE 'Alias-Name' 语句的基本语法如下:
1 |
<span class="pln">DETACH DATABASE </span><span class="str">'Alias-Name'</span><span class="pun">;</span> |
在这里,’Alias-Name' 与您之前使用 ATTACH 语句附加数据库时所用到的别名相同。 实例 假设在前面的章节中您已经创建了一个数据库,并给它附加了 'test' 和 'currentDB’,使用 .database 命令,我们可以看到:
1 2 3 4 5 6 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">databases seq name file </span><span class="pun">---</span> <span class="pun">---------------</span> <span class="pun">----------------------</span> <span class="lit">0</span><span class="pln"> main </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">sqlite</span><span class="pun">/</span><span class="pln">testDB</span><span class="pun">.</span><span class="pln">db </span><span class="lit">2</span><span class="pln"> test </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">sqlite</span><span class="pun">/</span><span class="pln">testDB</span><span class="pun">.</span><span class="pln">db </span><span class="lit">3</span><span class="pln"> currentDB </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">sqlite</span><span class="pun">/</span><span class="pln">testDB</span><span class="pun">.</span><span class="pln">db</span> |
现在,让我们尝试把 'currentDB' 从 testDB.db 中分离出来,如下所示:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> DETACH DATABASE </span><span class="str">'currentDB'</span><span class="pun">;</span> |
现在,如果检查当前附加的数据库,您会发现,testDB.db 仍与 'test' 和 'main' 保持连接。
1 2 3 4 5 6 7 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">databases seq name file </span><span class="pun">---</span> <span class="pun">---------------</span> <span class="pun">----------------------</span> <span class="lit">0</span><span class="pln"> main </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">sqlite</span><span class="pun">/</span><span class="pln">testDB</span><span class="pun">.</span><span class="pln">db </span><span class="lit">2</span><span class="pln"> test </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">sqlite</span><span class="pun">/</span><span class="pln">testDB</span><span class="pun">.</span><span class="pln">db from:<a href="http://www.runoob.com/sqlite/sqlite-detach-database.html">http://www.runoob.com/sqlite/sqlite-detach-database.html</a></span> |
假设这样一种情况,当在同一时间有多个数据库可用,您想使用其中的任何一个。SQLite 的 ATTACH DATABASE 语句是用来选择一个特定的数据库,使用该命令后,所有的 SQLite 语句将在附加的数据库下执行。 语法 SQLite 的 ATTACH DATABASE 语句的基本语法如下:
1 |
<span class="pln">ATTACH DATABASE </span><span class="str">'DatabaseName'</span> <span class="typ">As</span> <span class="str">'Alias-Name'</span><span class="pun">;</span> |
如果数据库尚未被创建,上面的命令将创建一个数据库,如果数据库已存在,则把数据库文件名称与逻辑数据库 'Alias-Name' 绑定在一起。 实例 如果想附加一个现有的数据库 testDB.db,则 ATTACH DATABASE 语句将如下所示:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> ATTACH DATABASE </span><span class="str">'testDB.db'</span> <span class="kwd">as</span> <span class="str">'TEST'</span><span class="pun">;</span> |
使用 SQLite .database 命令来显示附加的数据库。
1 2 3 4 5 |
<span class="pln">sqlite</span><span class="pun">></span> <span class="pun">.</span><span class="pln">database seq name file </span><span class="pun">---</span> <span class="pun">---------------</span> <span class="pun">----------------------</span> <span class="lit">0</span><span class="pln"> main </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">sqlite</span><span class="pun">/</span><span class="pln">testDB</span><span class="pun">.</span><span class="pln">db </span><span class="lit">2</span><span class="pln"> test </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">sqlite</span><span class="pun">/</span><span class="pln">testDB</span><span class="pun">.</span><span class="pln">db</span> |
数据库名称 main 和 temp 被保留用于主数据库和存储临时表及其他临时数据对象的数据库。这两个数据库名称可用于每个数据库连接,且不应该被用于附加,否则将得到一个警告消息,如下所示:
1 2 3 4 5 6 7 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> ATTACH DATABASE </span><span class="str">'testDB.db'</span> <span class="kwd">as</span> <span class="str">'TEMP'</span><span class="pun">;</span> <span class="typ">Error</span><span class="pun">:</span><span class="pln"> database TEMP </span><span class="kwd">is</span><span class="pln"> already </span><span class="kwd">in</span> <span class="kwd">use</span><span class="pln"> sqlite</span><span class="pun">></span><span class="pln"> ATTACH DATABASE </span><span class="str">'testDB.db'</span> <span class="kwd">as</span> <span class="str">'main'</span><span class="pun">;</span> <span class="typ">Error</span><span class="pun">:</span><span class="pln"> database TEMP </span><span class="kwd">is</span><span class="pln"> already </span><span class="kwd">in</span> <span class="kwd">use from:<a href="http://www.runoob.com/sqlite/sqlite-attach-database.html">http://www.runoob.com/sqlite/sqlite-attach-database.html</a></span> |
SQLite 的 sqlite3 命令被用来创建新的 SQLite 数据库。您不需要任何特殊的权限即可创建一个数据。 语法 sqlite3 命令的基本语法如下:
1 |
<span class="pln">$sqlite3 </span><span class="typ">DatabaseName</span><span class="pun">.</span><span class="pln">db</span> |
通常情况下,数据库名称在 RDBMS 内应该是唯一的。 实例 如果您想创建一个新的数据库 <testDB.db>,SQLITE3 语句如下所示:
1 2 3 4 5 |
<span class="pln">$sqlite3 testDB</span><span class="pun">.</span><span class="pln">db </span><span class="typ">SQLite</span><span class="pln"> version </span><span class="lit">3.7</span><span class="pun">.</span><span class="lit">15.2</span> <span class="lit">2013</span><span class="pun">-</span><span class="lit">01</span><span class="pun">-</span><span class="lit">09</span> <span class="lit">11</span><span class="pun">:</span><span class="lit">53</span><span class="pun">:</span><span class="lit">05</span> <span class="typ">Enter</span> <span class="str">".help"</span> <span class="kwd">for</span><span class="pln"> instructions </span><span class="typ">Enter</span><span class="pln"> SQL statements terminated </span><span class="kwd">with</span><span class="pln"> a </span><span class="str">";"</span><span class="pln"> sqlite</span><span class="pun">></span> |
上面的命令将在当前目录下创建一个文件 testDB.db。该文件将被 SQLite 引擎用作数据库。如果您已经注意到 sqlite3 命令在成功创建数据库文件之后,将提供一个 sqlite> 提示符。 一旦数据库被创建,您就可以使用 SQLite 的 .databases 命令来检查它是否在数据库列表中,如下所示:
1 2 3 4 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">databases seq name file </span><span class="pun">---</span> <span class="pun">---------------</span> <span class="pun">----------------------</span> <span class="lit">0</span><span class="pln"> main </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">sqlite</span><span class="pun">/</span><span class="pln">testDB</span><span class="pun">.</span><span class="pln">db</span> |
您可以使用 SQLite .quit 命令退出 sqlite 提示符,如下所示:
1 2 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">quit $</span> |
.dump 命令 您可以在命令提示符中使用 SQLite .dump 点命令来导出完整的数据库在一个文本文件中,如下所示:
1 |
<span class="pln">$sqlite3 testDB</span><span class="pun">.</span><span class="pln">db </span><span class="pun">.</span><span class="kwd">dump</span> <span class="pun">></span><span class="pln"> testDB</span><span class="pun">.</span><span class="pln">sql</span> |
上面的命令将转换整个 testDB.db 数据库的内容到 SQLite 的语句中,并将其转储到 ASCII 文本文件 testDB.sql 中。您可以通过简单的方式从生成的 testDB.sql 恢复,如下所示:
1 |
<span class="pln">$sqlite3 testDB</span><span class="pun">.</span><span class="pln">db </span><span class="pun"><</span><span class="pln"> testDB</span><span class="pun">.</span><span class="pln">sql</span> |
此时的数据库是空的,一旦数据库中有表和数据,您可以尝试上述两个程序。现在,让我们继续学习下一章。 from:http://www.runoob.com/sqlite/sqlite-create-database.html
View Details本章将向您讲解 SQLite 编程人员所使用的简单却有用的命令。这些命令被称为 SQLite 的点命令,这些命令的不同之处在于它们不以分号 ; 结束。 让我们在命令提示符下键入一个简单的 sqlite3 命令,在 SQLite 命令提示符下,您可以使用各种 SQLite 命令。
1 2 3 4 |
<span class="pln">$ sqlite3 </span><span class="typ">SQLite</span><span class="pln"> version </span><span class="lit">3.3</span><span class="pun">.</span><span class="lit">6</span> <span class="typ">Enter</span> <span class="str">".help"</span> <span class="kwd">for</span><span class="pln"> instructions sqlite</span><span class="pun">></span> |
如需获取可用的点命令的清单,可以在任何时候输入 ".help"。例如:
1 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">help</span> |
上面的命令会显示各种重要的 SQLite 点命令的列表,如下所示: 命令 描述 .backup ?DB? FILE 备份 DB 数据库(默认是 "main")到 FILE 文件。 .bail ON|OFF 发生错误后停止。默认为 OFF。 .databases 列出附加数据库的名称和文件。 .dump ?TABLE? 以 SQL 文本格式转储数据库。如果指定了 TABLE 表,则只转储匹配 LIKE 模式的 TABLE 表。 .echo ON|OFF 开启或关闭 echo 命令。 .exit 退出 SQLite 提示符。 .explain ON|OFF 开启或关闭适合于 EXPLAIN 的输出模式。如果没有带参数,则为 EXPLAIN on,及开启 EXPLAIN。 .header(s) ON|OFF 开启或关闭头部显示。 .help 显示消息。 .import FILE TABLE 导入来自 FILE 文件的数据到 TABLE 表中。 .indices ?TABLE? 显示所有索引的名称。如果指定了 TABLE 表,则只显示匹配 LIKE 模式的 TABLE 表的索引。 […]
View DetailsSQLite 是遵循一套独特的称为语法的规则和准则。本教程列出了所有基本的 SQLite 语法,向您提供了一个 SQLite 快速入门。 大小写敏感性 有个重要的点值得注意,SQLite 是不区分大小写的,但也有一些命令是大小写敏感的,比如 GLOB 和 glob 在 SQLite 的语句中有不同的含义。 注释 SQLite 注释是附加的注释,可以在 SQLite 代码中添加注释以增加其可读性,他们可以出现在任何空白处,包括在表达式内和其他 SQL 语句的中间,但它们不能嵌套。 SQL 注释以两个连续的 "-" 字符(ASCII 0x2d)开始,并扩展至下一个换行符(ASCII 0x0a)或直到输入结束,以先到者为准。 您也可以使用 C 风格的注释,以 "/*" 开始,并扩展至下一个 "*/" 字符对或直到输入结束,以先到者为准。SQLite的注释可以跨越多行。
1 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">help </span><span class="pun">--</span> <span class="typ">This</span> <span class="kwd">is</span><span class="pln"> a single line comment</span> |
SQLite 语句 所有的 SQLite 语句可以以任何关键字开始,如 SELECT、INSERT、UPDATE、DELETE、ALTER、DROP 等,所有的语句以分号(;)结束。 SQLite ANALYZE 语句:
1 2 3 4 5 |
<span class="pln">ANALYZE</span><span class="pun">;</span> <span class="kwd">or</span><span class="pln"> ANALYZE database_name</span><span class="pun">;</span> <span class="kwd">or</span><span class="pln"> ANALYZE database_name</span><span class="pun">.</span><span class="pln">table_name</span><span class="pun">;</span> |
SQLite AND/OR 子句:
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE CONDITION</span><span class="pun">-</span><span class="lit">1</span> <span class="pun">{</span><span class="pln">AND</span><span class="pun">|</span><span class="pln">OR</span><span class="pun">}</span><span class="pln"> CONDITION</span><span class="pun">-</span><span class="lit">2</span><span class="pun">;</span> |
SQLite ALTER TABLE 语句:
1 |
<span class="pln">ALTER TABLE table_name ADD COLUMN column_def</span><span class="pun">...;</span> |
SQLite ALTER TABLE 语句(Rename):
1 |
<span class="pln">ALTER TABLE table_name RENAME TO new_table_name</span><span class="pun">;</span> |
SQLite ATTACH DATABASE 语句:
1 |
<span class="pln">ATTACH DATABASE </span><span class="str">'DatabaseName'</span> <span class="typ">As</span> <span class="str">'Alias-Name'</span><span class="pun">;</span> |
SQLite BEGIN TRANSACTION 语句:
1 2 3 |
<span class="kwd">BEGIN</span><span class="pun">;</span> <span class="kwd">or</span> <span class="kwd">BEGIN</span><span class="pln"> EXCLUSIVE TRANSACTION</span><span class="pun">;</span> |
SQLite BETWEEN 子句:
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE column_name BETWEEN val</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> AND val</span><span class="pun">-</span><span class="lit">2</span><span class="pun">;</span> |
SQLite COMMIT 语句:
1 |
<span class="pln">COMMIT</span><span class="pun">;</span> |
SQLite CREATE INDEX 语句:
1 2 |
<span class="pln">CREATE INDEX index_name ON table_name </span><span class="pun">(</span><span class="pln"> column_name COLLATE NOCASE </span><span class="pun">);</span> |
SQLite CREATE UNIQUE INDEX […]
View Details