SQLite - Commands
The SQLite provides a simple command-line utility named sqlite3 which allows the user to execute SQL statements manually against an SQLite database. This chapter is a brief introduction of sqlite3 dot ( . ) commands.
- A dot-command must begin with the "." at the left margin with no preceding whitespace.
- The dot-command must be entirely contained on a single input line.
- A dot-command cannot occur in the middle of an ordinary SQL statement. In other words, a dot-command cannot occur at a continuation prompt.
- Dot-commands do not recognize comments.
All the available dot(.) commands are listed below:
S.N | Commands & Description |
---|---|
1. |
.archive ... Manage SQL archives |
2. |
.auth ON|OFF Show authorizer callbacks |
3. |
.backup ?DB? FILE Backup DB (default "main") to FILE |
4. |
.bail on|off Stop after hitting an error. Default OFF |
5. |
.binary on|off Turn binary output on or off. Default OFF |
6. |
.cd DIRECTORY Change the working directory to DIRECTORY |
7. |
.changes on|off Show number of rows changed by SQL |
8. |
.check GLOB Fail if output since .testcase does not match |
9. |
.clone NEWDB Clone data into NEWDB from the existing database |
10. |
.connection [close] [#] Open or close an auxiliary database connection |
11. |
.databases List names and files of attached databases |
12. |
.dbconfig ?op? ?val? List or change sqlite3_db_config() options |
13. |
.dbinfo ?DB? Show status information about the database |
14. |
.dump ?OBJECTS? Render database content as SQL |
15. |
.echo on|off Turn command echo on or off |
16. |
.eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN |
17. |
.excel Display the output of next command in spreadsheet |
18. |
.exit ?CODE? Exit this program with return-code CODE |
19. |
.expert EXPERIMENTAL. Suggest indexes for queries |
20. |
.explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto |
21. |
.filectrl CMD ... Run various sqlite3_file_control() operations |
22. |
.fullschema ?--indent? Show schema and the content of sqlite_stat tables |
23. |
.headers on|off Turn display of headers on or off |
24. |
.help ?-all? ?PATTERN? Show help text for PATTERN |
25. |
.import FILE TABLE Import data from FILE into TABLE |
26. |
.imposter INDEX TABLE Create imposter table TABLE on index INDEX |
27. |
.indexes ?TABLE? Show names of indexes |
28. |
.limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT |
29. |
.lint OPTIONS Report potential schema issues. |
30. |
.load FILE ?ENTRY? Load an extension library |
31. |
.log FILE|off Turn logging on or off. FILE can be stderr/stdout |
32. |
.mode MODE ?TABLE? Set output mode where MODE is one of the following:
|
33. |
.nonce STRING Disable safe mode for one command if the nonce matches |
34. |
.nullvalue STRING Use STRING in place of NULL values |
35. |
.once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE |
36. |
.open ?OPTIONS? ?FILE? Close existing database and reopen FILE |
37. |
.output ?FILE? Send output to FILE or stdout if FILE is omitted |
38. |
.parameter CMD ... Manage SQL parameter bindings |
39. |
.print STRING... Print literal STRING |
40. |
.progress N Invoke progress handler after every N opcodes |
41. |
.prompt MAIN CONTINUE Replace the standard prompts |
42. |
.quit Exit this program |
43. |
.read FILE Read input from FILE |
44. |
.recover Recover as much data as possible from corrupt db. |
45. |
.restore ?DB? FILE Restore content of DB (default "main") from FILE |
46. |
.save FILE Write in-memory database into FILE |
47. |
.scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off |
48. |
.schema ?PATTERN? Show the CREATE statements matching PATTERN |
49. |
.selftest ?OPTIONS? Run tests defined in the SELFTEST table |
50. |
.separator COL ?ROW? Change the column and row separators |
51. |
.session ?NAME? CMD ... Create or control sessions |
52. |
.sha3sum ... Compute a SHA3 hash of database content |
53. |
.shell CMD ARGS... Run CMD ARGS... in a system shell |
54. |
.show Show the current values for various settings |
55. |
.stats ?ARG? Show stats or turn stats on or off |
56. |
.system CMD ARGS... Run CMD ARGS... in a system shell |
57. |
.tables ?TABLE? List names of tables matching LIKE pattern TABLE |
58. |
.testcase NAME Begin redirecting output to 'testcase-out.txt' |
59. |
.testctrl CMD ... Run various sqlite3_test_control() operations |
60. |
.timeout MS Try opening locked tables for MS milliseconds |
61. |
.timer on|off Turn SQL timer on or off |
62. |
.trace ?OPTIONS? Output each SQL statement as it is run |
63. |
.vfsinfo ?AUX? Information about the top-level VFS |
64. |
.vfslist List all available VFSes |
65. |
.vfsname ?AUX? Print the name of the VFS stack |
66. |
.width NUM1 NUM2 ... Set minimum column widths for columnar output |
Formatting Output
The following sequence of dot commands can be used to format the output:
sqlite> .header on sqlite> .mode column sqlite> .timer on sqlite>
The above setting will produce the output in the following format.
EmpID Name City Age Salary -------- ---------- ----------- ---------- ---------- 1 John London 25 3000.0 2 Marry New York 24 2750.0 3 Jo Paris 27 2800.0 4 Kim Amsterdam 30 3100.0 5 Ramesh New Delhi 28 3000.0 6 Huang Beijing 28 2800.0 CPU Time: user 0.000000 sys 0.000000
The sqlite_master Table
The master table holds the key information about the database tables and it is called sqlite_master. The following dot command can be used to see its schema:
sqlite> .schema sqlite_master
This will produce the result which will be similar to:
CREATE TABLE sqlite_master ( type text, name text, tbl_name text, rootpage integer, sql text );