1. 程式人生 > >SQLite匯出整個資料庫/匯出某個表到SQL檔案

SQLite匯出整個資料庫/匯出某個表到SQL檔案

突然想匯出SQLite資料庫的某個表的資料,然後就記錄一下:

執行“sqlite3.exe”,我們可能用到下面幾個命令:
sqlite> .help
.dump ?TABLE? ...      Dump the database in an SQL text format
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.exit                  Exit this program
.help                  Show this message
.open ?--new? ?FILE?   Close existing database and reopen FILE
                         The --new starts with an empty file
.output ?FILENAME?     Send output to FILENAME or stdout
.quit                  Exit this program
.read FILENAME         Execute SQL in FILENAME
.tables ?TABLE?        List names of tables
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.
sqlite>

假設我們有一個SQLite資料庫檔案,名為db.sqlite3,這個資料庫中有2個表,分別為tb1和tb2,
我們匯出整個資料庫到db.SQL檔案的方式:
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open db.sqlite3
sqlite> .output db.SQL
sqlite> .dump
sqlite> .exit

我們匯出表tb1到db.SQL檔案的方式:
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open db.sqlite3
sqlite> .output db.SQL
sqlite> .dump tb1
sqlite> .exit
完。