To do well, you must first sharpen your tools. Learning SQLite's command line tool is very helpful for us to learn SQLite itself. The most basic one is that it makes learning SQLite easier and more enjoyable. To put it bluntly, the official download website of SQLite provides a command line tool that supports multiple platforms. Using this tool, we can complete most common SQLite operations, just like sqlplus in Oracle. The following lists the built-in commands for this tool:
Command name |
Command description |
. Help |
List all built-in commands. |
. Backup dbname File |
Back up a specified database to a specified file. The default value is the currently connected main database. |
. Databases |
Lists all the attached database names and file names in the current connection. |
. Dump tablename... |
Dump is currently connected to the main database in SQL text format. If the table name is specified, it is only a data table that matches the dump and table name. The tablename parameter supports wildcards supported by the like expression. |
. Echo on | off |
Turn on or off the display output. |
. Exit |
Exit currentProgram. |
. Explain on | off |
Enable or disable the select output of the current connection in the form of human readable. |
. Header (s) on | off |
Whether to display the column title when the select result is displayed. |
. Import file table |
Import the data of the specified file to the specified table. |
. Indices tablename |
Display the names of all indexes. If the table name is specified, only the indexes of the data tables matching the table name are displayed. The tablename parameter supports wildcards supported by the like expression. |
. Log File | off |
Enable or disable the log function. file can output stdout for standard output or stderr for standard error output. |
. Mode mode tablename |
Set the output mode. The most common mode is column mode, which enables the select output column to be left aligned. |
. Nullvalue string |
Use the specified string to display the null value. |
. Output Filename |
Redirects all output of the current command to the specified file. |
. Output stdout |
Redirects all output of the current command to the standard output (screen ). |
. Quit |
Exit the current program. |
. Read filename |
Execute the SQL statement in the specified file. |
. Restore dbname File |
Restore the database from the specified file. The default value is the main database. You can also specify other database names. The specified database becomes the currently connected attached database. |
. Schema tablename |
The table creation statement is displayed. If the table name is specified, only the table creation statements matching the table name are displayed. The tablename parameter supports wildcards supported by the like expression. |
. Separator string |
Change the delimiter between output mode and. Import fields. |
. Show |
Displays the current values of various settings. |
. Tables tablename |
Lists all the table names of the main database in the current connection. If the table name is specified, only the table names matching the table name are displayed. The tablename parameter supports wildcards supported by the like expression. |
. Width num1 num2... |
When mode is column, set the width of each field. Note: The Parameter order of this command indicates the field output order. |
See the following common examples:
1). Back up and restore the database.
-- Create a data table in the currently connected main database, and then use the. Backup command to back up the main database to the D:/mydb. DB file.
SQLite> Create Table mytable (first_col integer );
SQLite>. Backup'D:/mydb. db'
SQLite>. Exit
--Execute sqlite3.exe in the command line window to re-establish a connection with SQLite.
-- Restore data from the backup file D:/mydb. dB to the currently connected main database, and then run the. Tables command to view the mytable table.
SQLite>. Restore'D:/mydb. db'
SQLite>. Tables
Mytable
2) dump data table creation statement to the specified file.
-- First redirect the current output of the command line to D:/myoutput.txt, and then output the declaration statement of the previously created mytable table to this file.
SQLite>. OutputD:/myoutput.txt
SQLite>. DumpMytabl %
SQLite>. Exit
-- Use NotePad to open the target file in the DOS environment.
D: \>NotepadMyoutput.txt
3) display all the currently connected attached databases and main databases.
SQLite>Attach Database'D:/mydb. db'AsMydb;
SQLite>. Databases
SEQ name file
------------------------------------------
0 main
2 mydb D: \ mydb. DB
4). display all data tables in the main database.
SQLite>. Tables
Mytable
5). All indexes of the data table matching the table name mytabl % are displayed.
SQLite> Create index myindex on mytable (first_col );
SQLite>. IndicesMytabl%
Myindex
6). displays the schema information of the data table that matches the table name mytable %.
-- The index information dependent on the table is also output.
SQLite>. SchemaMytabl%
Create Table mytable (first_col integer );
Create index myindex on mytable (first_col );
7). Format and display the select output information.
-- Insert Test Data
SQLite> insert into mytable values (1 );
SQLite> insert into mytable values (2 );
SQLite> insert into mytable values (3 );
-- Note that the output format of the select result set is not set.
SQLite> select * From mytable;
1
2
3
-- Display the column name of the select result set.
-- Each field is displayed as a column.
-- Set the display width of the first output column to 10.
SQLite>. HeaderOn
SQLite>. ModeColumn
SQLite>. Width10
SQLite> select * From mytable;
First_col
----------
1
2
3