官网的文档结构十分恶劣,大概翻了一下,提供入门指引。
0. sqlite的安装
根据自身情况,在官网下载32位/64位的dll文件以及sqlite-tools-win32-x86-3240000.zip 。
sqlite无需安装,本体是下载的dll文件(def文件是什么鬼我也不懂), tool比如sqlite3.exe只是操作sqlite的工具。
两者解压到同一个文件夹下即可。 如C:sqlite.
通过cmd来到解压路径,输入sqlite3,如果看到相应的进入shell窗口的提示,则表示一切顺利。
如果想要在全局cmd环境下启用sqlite3命令,将文件夹路径加入PATH环境变量即可。
由于sqlite太轻量,也有将sqlite3.exe和sqlite.dll放到C:WindowsSystem32下的,无需修改PATH变量。
1. Getting Started
使用sqlite十分简单。如上做好安装之后,在cmd输入sqlite3 xxx.db 即可创建(进入)名为xxx的数据库。
sqlite并没有账户控制,所以也不用纠结用户名和密码的设置。
刚进入shell,sqlite还没有创建xxx.db的文件,当用户做出有效操作后,sqlite才会在当前路径下创建一个名为xxx.db的文件。这个文件即是传统意义上的一个库了。
如果一开始不指定dbname,直接在cmd输入sqlite,也可以进入shell。此时sqlite会隐式创建一个临时数据库,用户依然可以在shell中执行创建表等操作,但是当用户退出shell时,这个隐式的数据库也会跟着销毁。若想要保存这期间的操作,退出之前保存到一个文件中即可。命令为: .save xxx.db
Quick Reference:
创建(或打开)数据库(名为name.db):
> sqlite3 name.db
退出数据库:
>>.quit
查看数据库:
>> .databases
查看创建表:
>> .tables
查看表结构:
>> .schema tb1
CURD操作:(都是常规的SQL语句)
>> SELECT * FROM tb1;
>> UPDATE tb1 SET ...;
>> 略
2. sqlite 的dot commands
sqlite的dot(即".")操作通常用于修改查询的输出或执行特定的预打包的查询语句,属于sqlite的自身的操作指令。
以下是dot命令列表,可以在shell中输入.help查看。个人常用的命令标红显示。
sqlite> .help .archive ... Manage SQL archives: ".archive --help" for details .auth ON|OFF Show authorizer callbacks .backup ?DB? FILE Backup DB (default "main") to FILE Add "--append" to open using appendvfs. .bail on|off Stop after hitting an error. Default 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? ... Dump the database in an SQL text format If TABLE specified, only dump tables matching LIKE pattern TABLE. .echo on|off Turn command echo on or off .eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN .excel Display the output of next command in a spreadsheet .exit Exit this program .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 Show this message .import FILE TABLE Import data from FILE into TABLE .imposter INDEX TABLE Create imposter table TABLE on index INDEX .indexes ?TABLE? Show names of all indexes If TABLE specified, only show indexes for tables matching LIKE pattern TABLE. .iotrace FILE Enable I/O diagnostic logging to FILE .limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT .lint OPTIONS Report potential schema issues. Options: fkey-indexes Find missing foreign key indexes .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 where MODE is one of: ascii Columns/rows delimited by 0x1F and 0x1E csv Comma-separated values column Left-aligned columns. (See .width) html HTML <table> code insert SQL insert statements for TABLE line One value per line list Values delimited by "|" quote Escape answers as for SQL tabs Tab-separated values tcl TCL list elements .nullvalue STRING Use STRING in place of NULL values .once (-e|-x|FILE) Output for the next SQL command only to FILE or invoke system text editor (-e) or spreadsheet (-x) on the output. .open ?OPTIONS? ?FILE? Close existing database and reopen FILE The --new option starts with an empty file Other options: --readonly --append --zip .output ?FILE? Send output to FILE or stdout .print STRING... Print literal STRING .prompt MAIN CONTINUE Replace the standard prompts .quit Exit this program .read FILENAME Execute SQL in FILENAME .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 Add --indent for pretty-printing .selftest ?--init? Run tests defined in the SELFTEST table .separator COL ?ROW? Change the column separator and optionally the row separator for both the output mode and .import .session CMD ... Create or control sessions .sha3sum ?OPTIONS...? 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 If TABLE specified, only list tables matching LIKE pattern TABLE. .testcase NAME Begin redirecting output to 'testcase-out.txt' .timeout MS Try opening locked tables for MS milliseconds .timer on|off Turn SQL timer on or off .trace FILE|off 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 Negative values right-justify sqlite>
shell中的SQL语句比较随意, 只有输入分号的时候才会执行。 但是dot命令必须以.开头,回车即执行。
改变输出格式的几个操作:
sqlite> .mode list sqlite> select * from tbl1; hello|10 goodbye|20
sqlite> .mode line sqlite> select * from tbl1; one = hello two = 10 one = goodbye two = 20
sqlite> .mode column sqlite> select * from tbl1; one two ---------- ---------- hello 10 goodbye 20 sqlite>
sqlite> .header off sqlite> select * from tbl1; hello 10 goodbye 20 sqlite>
其他常见操作:
- 将结果写入文件
- CSV import
- CSV Export
- Excel Export
....
操作太杂,如果有更多需求如Index等请直接查看文档。
https://sqlite.org/cli.html