SQLite classic ...login
SQLite classic tutorial
author:php.cn  update time:2022-04-13 17:05:02

SQLite commands


This chapter will teach you simple but useful commands used by SQLite programmers. These commands are known as SQLite's dot commands, and these commands differ in that they do not end with a semicolon (;).

Let us type a simple sqlite3 command in the 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 the available point commands list, you can enter ".help" at any time. For example:

sqlite>.help

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

CommandDescription
.backup ?DB? FILEBackup the DB database (default is "main") to the FILE file.
.bail ON|OFFStop after an error occurs. Default is OFF.
.databasesLists the names and files of attached databases.
.dump ?TABLE?Dumps the database in SQL text format. If TABLE tables are specified, only TABLE tables matching the LIKE pattern are dumped.
.echo ON|OFFTurn on or off the echo command.
.exitExit the SQLite prompt.
.explain ON|OFF Turn on or off the output mode suitable for EXPLAIN. If there is no parameter, it is EXPLAIN on, and EXPLAIN is turned on.
.header(s) ON|OFFTurn on or off the header display.
.helpDisplay message.
.import FILE TABLEImport data from the FILE file into the TABLE table.
.indices ?TABLE?Display the names of all indexes. If a TABLE table is specified, only indexes for TABLE tables matching the LIKE pattern are displayed.
.load FILE ?ENTRY?Load an extension library.
.log FILE|offTurn on or off the log. FILE file can be stderr (standard error)/stdout (standard output).
.mode MODESet the output mode, MODE can be one of the following:
  • csv Comma separated The value of

  • column left aligned column

  • html HTML<table> Code

  • insert SQL insert (insert) statement of TABLE table

  • line Each row A value

  • list A value separated by a .separator string

  • tabs by Tab Delimited values

  • tcl TCL list elements

.nullvalue STRINGOutputs the STRING string where the NULL value is.
.output FILENAMESend output to the FILENAME file.
.output stdoutSends output to the screen.
.print STRING...Output the STRING string verbatim.
.prompt MAIN CONTINUEReplaces the standard prompt.
.quitQuit the SQLite prompt.
.read FILENAMEExecute the SQL in the FILENAME file.
.schema ?TABLE?Display CREATE statement. If TABLE tables are specified, only TABLE tables matching the LIKE pattern are displayed.
.separator STRINGChange the output mode and the separator used by .import.
.showDisplays the current values ​​of various settings.
.stats ON|OFF Turn statistics on or off.
.tables ?PATTERN?Lists the names of tables that match the LIKE pattern.
.timeout MSAttempt to open locked table MS microseconds.
.width NUM NUMSets the column width for "column" mode.
.timer ON|OFF Turn on or off CPU timer measurement.

Let us try using the .show command to see the default settings of the SQLite command prompt.

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

Formatted output

You can use the following dot command to format the output into the format listed below in this tutorial:

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

The above settings will produce output in the following format:

ID                   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

The main table stores the key information of the database table and names it sqlite_master. To view the table summary, do the following:

sqlite>.schema sqlite_master

This will produce the following results:

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


php.cn