Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQLite Commands

SQLite Commands

This chapter explains the simple yet useful commands used by SQLite programmers. These commands are called SQLite dot commands, and what makes them different is that they do not end with a semicolon ;.

Let's type a simple sqlite3 command at the command prompt; at the SQLite command prompt, you can use various SQLite commands.



$ sqlite3

SQLite version 3.3.6

Enter ".help" for instructions

sqlite>

To get a list of available dot commands, you can type ".help" at any time. For example:



sqlite>.help

The above command displays a list of various important SQLite dot commands, as shown below:

Command Description
.backup ?DB? FILE Back up the DB database (default is "main") to the FILE.
.bail ON|OFF Stop after an error occurs. Default is OFF.
.databases List the names of databases and the files they are attached to.
.dump ?TABLE? Dump the database in SQL text format. If TABLE is specified, only dump the TABLE tables matching the LIKE pattern.
.echo ON|OFF Turn the echo command on or off.
.exit Exit the SQLite prompt.
.explain ON|OFF Turn the output mode suitable for EXPLAIN on or off. If no argument is given, it is EXPLAIN on, i.e., EXPLAIN is enabled.
.header(s) ON|OFF Turn header display on or off.
.help Show messages.
.import FILE TABLE Import data from the FILE into the TABLE.
.indices ?TABLE? Show the names of all indexes. If TABLE is specified, only show indexes for TABLE tables matching the LIKE pattern.
.load FILE ?ENTRY? Load an extension library.
.log FILE|off Turn logging on or off. The FILE can be stderr (standard error) / stdout (standard output).
.mode MODE Set the output mode; MODE can be one of the following:
  • csv Comma-separated values

  • column Left-aligned columns

  • html The HTML

    Code

  • insert SQL insert statements for the TABLE

  • line One value per line

  • list Values separated by the .separator string

  • tabs Values separated by Tab

  • tcl TCL list elements

  • | | .nullvalue STRING | Output the STRING in place of NULL values. | | .output FILENAME | Send output to the FILENAME. | | .output stdout | Send output to the screen. | | .print STRING... | Output the STRING verbatim. | | .prompt MAIN CONTINUE | Replace the standard prompts. | | .quit | Exit the SQLite prompt. | | .read FILENAME | Execute the SQL in the FILENAME. | | .schema ?TABLE? | Show the CREATE statements. If TABLE is specified, only show TABLE tables matching the LIKE pattern. | | .separator STRING | Change the separators used by the output mode and .import. | | .show | Show the current values of various settings. | | .stats ON\|OFF | Turn statistics on or off. | | .tables ?PATTERN? | List the names of tables matching the LIKE pattern. | | .timeout MS | Try opening a locked table for MS milliseconds. | | .width NUM NUM | Set column widths for "column" mode. | | .timer ON\|OFF | Turn the CPU timer on or off. |

    Let's try using the .show command to view 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 commands 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          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
    
    

    The sqlite_master Table

    The master table holds key information about the database tables and is named sqlite_master. To view the table summary, you can do as follows:

    
    
    sqlite>.schema sqlite_master
    
    

    This will produce the following result:

    
    
    CREATE TABLE sqlite_master (
    
      type text,
    
      name text,
    
      tbl_name text,
    
      rootpage integer,
    
      sql text
    
    );
    
    
    Source: Runoob Tutorial · Original modified: 2019-11-15T10:38:52 · This copy is for personal offline learning only; public distribution is prohibited.