.help .archive ..." /> .help .archive ..."/>

终端命令:掌握SQLite的基本操作

发表时间: 2019-06-10 23:36

sqlite命令

sqlite提供一个终端操作,支持很多命令,可以通过这些命令对sqlite数据库进行维护。


命令清单

可以通过sqlite命令进入终端,会提示.help来获取命令帮助

SQLite version 3.28.0 2019-04-16 19:49:53Enter ".help" for usage hints.sqlite> .help.archive ... Manage SQL archives.auth ON|OFF Show authorizer callbacks.backup ?DB? FILE Backup DB (default "main") to FILE #备份 DB 数据库(默认是 "main")到 FILE 文件。.bail on|off Stop after hitting an error. Default OFF #发生错误后停止。默认为 OFF。.binary on|off Turn binary output on or off. Default OFF.cd DIRECTORY Change the working directory to DIRECTORY.changes on|off Show number of rows changed by SQL.check GLOB Fail if output since .testcase does not match.clone NEWDB Clone data into NEWDB from the existing database.databases List names and files of attached databases #列出数据库的名称及其所依附的文件。.dbconfig ?op? ?val? List or change sqlite3_db_config() options.dbinfo ?DB? Show status information about the database.dump ?TABLE? ... Render all database content as SQL #以 SQL 文本格式转储数据库;如果指定了 TABLE 表,则只转储匹配 LIKE 模式的 TABLE 表。.echo on|off Turn command echo on or off #开启或关闭 echo 命令。.eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN #	开启或关闭适合于 EXPLAIN 的输出模式。支持模式:off|on|trace|trigger|full.excel Display the output of next command in a spreadsheet.exit ?CODE? Exit this program with return-code CODE #退出 SQLite 提示符。.expert EXPERIMENTAL. Suggest indexes for specified queries.fullschema ?--indent? Show schema and the content of sqlite_stat tables.headers on|off Turn display of headers on or off #开启或关闭头部显示。.help ?-all? ?PATTERN? Show help text for PATTERN.import FILE TABLE Import data from FILE into TABLE #导入来自 FILE 文件的数据到 TABLE 表中。.imposter INDEX TABLE Create imposter table TABLE on index INDEX.indexes ?TABLE? Show names of indexes #查看所有索引的名称.limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT.lint OPTIONS Report potential schema issues..load FILE ?ENTRY? Load an extension library #加载一个扩展库。.log FILE|off Turn logging on or off. FILE can be stderr/stdout.mode MODE ?TABLE? Set output mode 设置输出模式,MODE 可以是下列之一: csv 逗号分隔的值;column 左对齐的列;html HTML 的 <table> 代码;insert TABLE 表的 SQL 插入(insert)语句;line 每行一个值;list 由 .separator 字符串分隔的值;tabs 由 Tab 分隔的值;tcl TCL 列表元素.nullvalue STRING Use STRING in place of NULL values #在 NULL 值的地方输出 STRING 字符串。.once (-e|-x|FILE) Output for the next SQL command only to FILE.open ?OPTIONS? ?FILE? Close existing database and reopen FILE.output ?FILE? Send output to FILE or stdout if FILE is omitted #发送输出到 FILENAME 文件。.output stdout	发送输出到屏幕。.parameter CMD ... Manage SQL parameter bindings.print STRING... Print literal STRING #逐字地输出 STRING 字符串。.progress N Invoke progress handler after every N opcodes.prompt MAIN CONTINUE Replace the standard prompts #替换标准提示符。.quit Exit this program #退出 SQLite 提示符。.read FILE Read input from FILE #执行 FILENAME 文件中的 SQL。.restore ?DB? FILE Restore content of DB (default "main") from FILE.save FILE Write in-memory database into FILE.scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off.schema ?PATTERN? Show the CREATE statements matching PATTERN #显示 CREATE 语句。如果指定了 TABLE 表,则只显示匹配 LIKE 模式的 TABLE 表。.selftest ?OPTIONS? Run tests defined in the SELFTEST table.separator COL ?ROW? Change the column and row separators #改变输出模式和 .import 所使用的分隔符。.sha3sum ... Compute a SHA3 hash of database content.shell CMD ARGS... Run CMD ARGS... in a system shell.show Show the current values for various settings #显示各种设置的当前值。.stats ?on|off? Show stats or turn stats on or off #开启或关闭统计。.system CMD ARGS... Run CMD ARGS... in a system shell.tables ?TABLE? List names of tables matching LIKE pattern TABLE #列出匹配 LIKE 模式的表的名称。.testcase NAME Begin redirecting output to 'testcase-out.txt' #将输出重定向到testcase-out.txt的文件中.timeout MS Try opening locked tables for MS milliseconds #尝试打开锁定的表 MS 毫秒。.timer on|off Turn SQL timer on or off #开启或关闭 CPU 定时器。.trace ?OPTIONS? Output each SQL statement as it is run.vfsinfo ?AUX? Information about the top-level VFS.vfslist List all available VFSes.vfsname ?AUX? Print the name of the VFS stack.width NUM1 NUM2 ... Set column widths for "column" mode #为 "column" 模式设置列宽度。

查看当前显示设置

sqlite> .show

echo: off

eqp: off

explain: auto

headers: off

mode: list

nullvalue: ""

output: stdout

colseparator: "|"

rowseparator: "\n"

stats: off

width:

filename: 1.sqlite


格式化输出

sqlite> .header on #设置显示表头sqlite> .mode column #设置输出表格sqlite> .timer on #CPU计数器sqlite> select * from t1;

id name

---------- ----------

1 21123

2 123123

Run Time: real 0.001 user 0.000200 sys 0.000067


sqlite_master 表格

主表中保存数据库表的关键信息,并把它命名为 sqlite_master。

查看master的建表语句

sqlite> .schema sqlite_masterCREATE TABLE sqlite_master ( type text, name text, tbl_name text, rootpage integer, sql text);

查看表相关信息

select * from sqlite_master;

查看所有的表

sqlite> .tables

查看索引

如果不指定表名就是查看所有的索引

sqlite> create table t2 (id int primary key , name varchar(32) );sqlite> .indexes t2


查看建表语句

sqlite> .shema t2CREATE TABLE t2 (id int primary key , name varchar(32) );

导出SQL语句

sqlite> .dump t1 PRAGMA foreign_keys=OFF;BEGIN TRANSACTION;CREATE TABLE t1 ( id int , name varchar(20));INSERT INTO t1 VALUES(1,'21123');INSERT INTO t1 VALUES(2,'123123');COMMIT;

也可以直接到处到文件,只要指定导出的文件即可。

sqlite> .output backup.sqlsqlite> .dump t1 

导入sql

sqlite> .read backup.sql


备份数据库

sqlite> .backup 2.sqlite3


从TXT文本里面导入

有时候我们也希望txt文本里面的有规律的数据,可以直接导入到sqlite3数据库。

先指定字段分隔符。

sqlite3 db.sqlitesqlite> .separator ","sqlite> .import a.txt table1

导出到TXT文本

直接使用select语句直接查询出来就可以了,默认是以|作为分隔符

sqlite3 db.sqlite

sqlite> .separator ","

sqlite> .output a.txt

sqlite> select * from table1

sqlite> .exit

操作系统","author_description":"初学者之路张老师","price":48,"share_price":8.4,"sold":157,"column_id":"6680078352373514499","distribution_user_id":1578884314714125,"new_thumb_url":"http://p1.toutiaoimg.com/img/pgc-image/1555326771965bc06293006"}">
专栏
黑客必学之DOS操作系统
作者:初学者之路张老师
48币
157人已购
查看