sqlite-命令行工具使用說明

sqlite 命令行工具使用說明

一、進入命令行
linux 使用 sqlite3 命令,windows 雙擊 sqlite3.exe 進入;

二、簡單使用
#切換到db 目錄
.cd /Users/my/db/    
#關閉已經進入的db,重新打開 test.db 庫,如果 test.db 不存在則新建
.open test.db    #進入 test.db 庫
#查看 test.db 所有表
.tables
#創建 tb2 表
CREATE TABLE tb1 (f1 varchar(30) primary key,f2 text,f3 real);
CREATE TABLE tb2 (f1 varchar(30) primary key,f2 text,f3 real);
#插入記錄
insert into tb1 values('hello11',10,1);
insert into tb1 values('hello22',10,1);
insert into tb1 values('hello33',10,1);
insert into tb2 values('hello11',10,1);
insert into tb2 values('hello22',10,1);
insert into tb2 values('hello33',10,1);
#再次查看
.tables
---------------comand output---------------
sqlite> .tables
tb2
-------------------------------------------
#查看錶數據
sqlite> select * from tb2;
---------------comand output---------------
hello!|10|1.0
hello2|10|1.0
hello3|10|1.0
-------------------------------------------
#保存當前 db 到一個新 db(相當於備份整個庫),一下三個命令都可以
.save test_backup.db   #保存當前 test.db 所有信息到 test_backup.db
sqlite> .save test_save.db        #保存命令
sqlite> .backup test_backup.db    #備份命令
sqlite> .clone test_clone.db    #克隆命令
---------------comand output---------------
tb2... done
sqlite_autoindex_tb2_1... done
sqlite> 
-------------------------------------------
#顯示建表腳本
---------------comand output---------------
sqlite> .schema        #查整庫
CREATE TABLE tb2 (f1 varchar(30) primary key,f2 text,f3 real);
CREATE TABLE tb1 (f1 varchar(30) primary key,f2 text,f3 real);
---------------comand output---------------
sqlite> .schema tb2    #查指定表
CREATE TABLE tb2 (f1 varchar(30) primary key,f2 text,f3 real);
-------------------------------------------
#顯示索引信息
sqlite> .indexes
sqlite_autoindex_tb1_1  sqlite_autoindex_tb2_1

#退出命令行
.quit

三、將數據庫備份爲 sql 腳本文件
使用 cd 命令,到你的 db 目錄
命令格式:  
    sqlite3 庫名 .dump | gizp -c >文件名

#將 test.db 庫導出爲 sql 腳本,並將文件使用 gzip 壓縮到 test.db.gz 文件中
sqlite3 test.db .dump | gzip -c >test_dump200403.sql.gz    

#不解壓查看 test_dump200403.sql.gz 內容(三個命令均可)
gunzip -c test.db.gz    #macbook 命令
zcat test.db.gz        #linux 命令
gzip -drc test_dump200403.sql.gz    #gzip 命令控制檯中不解壓查看
---------------comand output---------------
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE tb1 (f1 varchar(30) primary key,f2 text,f3 real);
INSERT INTO tb1 VALUES('hello11','10',1.0);
INSERT INTO tb1 VALUES('hello22','10',1.0);
INSERT INTO tb1 VALUES('hello33','10',1.0);
CREATE TABLE tb2 (f1 varchar(30) primary key,f2 text,f3 real);
INSERT INTO tb2 VALUES('hello11','10',1.0);
INSERT INTO tb2 VALUES('hello22','10',1.0);
INSERT INTO tb2 VALUES('hello33','10',1.0);
COMMIT;
-------------------------------------------

#解壓查看 slq 腳本
gunzip -c test_dump200403.sql.gz > test_dump200403.sql #解壓後將創建一個 test_dump200403.sql 文件,保留原文件
less test_dump200403.sql
---------------comand output---------------
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE tb1 (f1 varchar(30) primary key,f2 text,f3 real);
INSERT INTO tb1 VALUES('hello11','10',1.0);
INSERT INTO tb1 VALUES('hello22','10',1.0);
INSERT INTO tb1 VALUES('hello33','10',1.0);
CREATE TABLE tb2 (f1 varchar(30) primary key,f2 text,f3 real);
INSERT INTO tb2 VALUES('hello11','10',1.0);
INSERT INTO tb2 VALUES('hello22','10',1.0);
INSERT INTO tb2 VALUES('hello33','10',1.0);
COMMIT;
-------------------------------------------


四、將 dump 的文件直接還原爲數據庫
命令格式:
    zcat 壓縮文件名 | sqlite3 新庫名    #macbook 可能不好用
    gunzip 壓縮文件名 | sqlite3 新庫名    #兼容mac
#執行命令,創建 test_new.db 文件
gunzip -c test_dump200403.sql.gz | sqlite3 test_new.db

#驗證 test_new.db 庫
$ sqlite3    輸入命令
---------------comand output---------------
SQLite version 3.28.0 2019-04-15 14:49:49
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open test_new.db  #打開庫
sqlite> .tables    #看看所有表
tb1  tb2
sqlite> select * from tb1;
hello11|10|1.0
hello22|10|1.0
hello33|10|1.0
sqlite> .quit
-------------------------------------------


五、sqlite3 命令列表:(其中 ?DB? 表示操作的爲當前庫,即使用 .open 打開的庫;)
定義:
    ?DB? 表示操作的爲當前庫,即使用 .open 打開的庫;
    ?TABLE?    表名可選;
命令:
.archive ...                 Manage SQL archives(管理SQL存檔)
.auth ON|OFF                 Show authorizer callbacks(顯示授權者回調)
.backup ?DB? FILE            Backup DB (default "main") to FILE(備份數據庫(默認爲“ main”)到FILE)
.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(將工作目錄更改爲DIRECTORY)
.changes on|off              Show number of rows changed by SQL(顯示由SQL更改的行數)
.check GLOB                  Fail if output since .testcase does not match(由於.testcase的輸出不匹配,則檢查GLOB失敗)
.clone NEWDB                 Clone data into NEWDB from the existing database(將數據從現有數據庫克隆到NEWDB)
.databases                   List names and files of attached databases(列出附加數據庫的名稱和文件)
.dbconfig ?op? ?val?         List or change sqlite3_db_config() options(列出或更改sqlite3_db_config()選項)
.dbinfo ?DB?                 Show status information about the database(顯示有關數據庫的狀態信息)
.dump ?TABLE? ...            Render all database content as SQL(將所有數據庫內容呈現爲SQL)
.echo on|off                 Turn command echo on or off(打開或關閉命令回顯)
.eqp on|off|full|...         Enable or disable automatic EXPLAIN QUERY PLAN(啓用或禁用自動的EXPLAIN QUERY PLAN)
.excel                       Display the output of next command in a spreadsheet(在電子表格中顯示下一個命令的輸出)
.exit ?CODE?                 Exit this program with return-code CODE(使用返回碼CODE退出此程序)
.expert                      EXPERIMENTAL. Suggest indexes for specified queries(實驗。建議指定查詢的索引)
.fullschema ?--indent?       Show schema and the content of sqlite_stat tables(顯示模式和sqlite_stat表的內容)
.headers on|off              Turn display of headers on or off(打開或關閉標題顯示)
.help ?-all? ?PATTERN?       Show help text for PATTERN(顯示PATTERN的幫助文本)
.import FILE TABLE           Import data from FILE into TABLE(將數據從FILE導入TABLE)
.imposter INDEX TABLE        Create imposter table TABLE on index INDEX(在索引INDEX上創建imposter表TABLE)
.indexes ?TABLE?             Show names of indexes(顯示索引名稱)
.iotrace FILE                Enable I/O diagnostic logging to FILE(啓用對FILE的I / O診斷日誌記錄)
.limit ?LIMIT? ?VAL?         Display or change the value of an SQLITE_LIMIT(顯示或更改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(打開或關閉登錄。 FILE可以是stderr / stdout)
.mode MODE ?TABLE?           Set output mode(設定輸出模式)
.nullvalue STRING            Use STRING in place of NULL values(使用STRING代替NULL值)
.once (-e|-x|FILE)           Output for the next SQL command only to FILE(僅將下一個SQL命令的輸出輸出到FILE)
.open ?OPTIONS? ?FILE?       Close existing database and reopen FILE(關閉現有數據庫並重新打開FILE)
.output ?FILE?               Send output to FILE or stdout if FILE is omitted(將輸出發送到FILE或stdout(如果省略FILE))
.parameter CMD ...           Manage SQL parameter bindings(管理SQL參數綁定)
.print STRING...             Print literal STRING(打印文字STRING)
.progress N                  Invoke progress handler after every N opcodes(每N個操作碼後調用進度處理程序)
.prompt MAIN CONTINUE        Replace the standard prompts(主要內容替換標準提示)
.quit                        Exit this program(退出該程序)
.read FILE                   Read input from FILE(讀取輸入)
.restore ?DB? FILE           Restore content of DB (default "main") from FILE(恢復DB的內容(默認爲“ main”))
.save FILE                   Write in-memory database into FILE(將內存數據庫寫入FILE)
.scanstats on|off            Turn sqlite3_stmt_scanstatus() metrics on or off(打開或關閉sqlite3_stmt_scanstatus()指標)
.schema ?PATTERN?            Show the CREATE statements matching PATTERN(命令顯示數據庫或單個表(如果提供了可選的tablename參數)的完整建表腳本)
.selftest ?OPTIONS?          Run tests defined in the SELFTEST table(運行SELFTEST表中定義的測試)
.separator COL ?ROW?         Change the column and row separators(更改列和行分隔符)
.session ?NAME? CMD ...      Create or control sessions(創建或控制會話)
.sha3sum ...                 Compute a SHA3 hash of database content(計算數據庫內容的SHA3哈希)
.shell CMD ARGS...           Run CMD ARGS... in a system shell(在系統Shell中運行CMD ARGS ...)
.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(在系統外殼中運行CMD ARGS ...)
.tables ?TABLE?              List names of tables matching LIKE pattern TABLE(列出與LIKE模式TABLE匹配的表的名稱)
.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(打開或關閉SQL計時器)
.trace ?OPTIONS?             Output each SQL statement as it is run(選項?在運行時輸出每個SQL語句)
.vfsinfo ?AUX?               Information about the top-level VFS(有關頂級VFS的信息)
.vfslist                     List all available VFSes(列出所有可用的VFS)
.vfsname ?AUX?               Print the name of the VFS stack(打印VFS堆棧的名稱)
.width NUM1 NUM2 ...         Set column widths for "column" mode(設置“列”模式的列寬)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章