Latest web development tutorials

SQLite command

You will explain the simple SQLite programmers used commands used in this chapter there. These commands are called SQLite point command, except that they do not these commands with a semicolon (;) end.

Let's type a simplesqlite3 command at a command prompt in the SQLite command prompt, you can use various SQLite commands.

$ Sqlite3
SQLite version 3.3.6
Enter ".help" for instructions
sqlite>

To obtain a list of available commands point, you can enter at any time, ".help". E.g:

sqlite> .help

The above command will display a list of various important point SQLite command, as follows:

命令描述
.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 表的索引。
.load FILE ?ENTRY?加载一个扩展库。
.log FILE|off开启或关闭日志。FILE 文件可以是 stderr(标准错误)/stdout(标准输出)。
.mode MODE设置输出模式,MODE 可以是下列之一:
  • csv逗号分隔的值

  • column左对齐的列

  • htmlHTML 的 <table> 代码

  • insertTABLE 表的 SQL 插入(insert)语句

  • line每行一个值

  • list由 .separator 字符串分隔的值

  • tabs由 Tab 分隔的值

  • tclTCL 列表元素

.nullvalue STRING在 NULL 值的地方输出 STRING 字符串。
.output FILENAME发送输出到 FILENAME 文件。
.output stdout发送输出到屏幕。
.print STRING...逐字地输出 STRING 字符串。
.prompt MAIN CONTINUE替换标准提示符。
.quit退出 SQLite 提示符。
.read FILENAME执行 FILENAME 文件中的 SQL。
.schema ?TABLE?显示 CREATE 语句。如果指定了 TABLE 表,则只显示匹配 LIKE 模式的 TABLE 表。
.separator STRING改变输出模式和 .import 所使用的分隔符。
.show显示各种设置的当前值。
.stats ON|OFF开启或关闭统计。
.tables ?PATTERN?列出匹配 LIKE 模式的表的名称。
.timeout MS尝试打开锁定的表 MS 微秒。
.width NUM NUM为 "column" 模式设置列宽度。
.timer ON|OFF 开启或关闭 CPU 定时器测量。

Let's try using.show command to view the default settings SQLite command prompt.

sqlite> .show
     echo: off
  explain: off
  headers: off
     mode: column
nullvalue: ""
   output: stdout
separator: "|"
    width:
sqlite>
Make sure there are no spaces between sqlite> prompt and command points, otherwise it will not work.

Formatted output

You can use the following commands to format the output point format for this tutorial listed below:

sqlite> .header on
sqlite> .mode column
sqlite> .timer on
sqlite>

The above settings will produce output in the following format:

ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
CPU Time: user 0.000000 sys 0.000000

sqlite_master table

Critical information stored in the master table database table, and named itsqlite_master.To view the summary table, according to the following:

sqlite> .schema sqlite_master

This produces the following results:

CREATE TABLE sqlite_master (
  type text,
  name text,
  tbl_name text,
  rootpage integer,
  sql text
);