1. 程式人生 > >SQLite資料庫的掛接及常用命令

SQLite資料庫的掛接及常用命令

安裝:

官方網站下載最新的sqlite版本

官方的下載頁面提供了很多版本的下載…這裡介紹一下;

Source Code: 原始碼版本的下載

Documentation:  相關文件

Precompiled Binaries for Linux / Precompiled Binaries For Mac OS X /

Precompiled Binaries For Windows

Linux/Mac/Win版本的預編譯版本


如果你只是資料庫的使用者那麼下載已經編譯好的版本即可! 這裡我是Win使用者,下載的檔案為: sqlite3.exe

將這個檔案放到一個目錄中,這樣就完成了全部的sqlite資料庫的安裝!

新建:

在命令提示符下($為shell提示號), 測試的sqlite3.exe路徑為e:/sqlite3/sqlite3.exe

網上也提供了很多方法,但好象都有點問題,經過摸索結論為以下幾種方法,但不確定是否受作業系統系統影響!

生成資料庫檔案後掛接
$> sqlite3.exe test.db ;

$> sqlite3.exe test.db

直接掛接生成

$> sqlite3.exe test.db;

選擇性掛接 挑選已經存在的資料檔案掛接
$> sqlite3.exe test.db 或者

$> sqlite3.exe e:/sqlite3/test.db

掛接好資料庫後,就可以通過命令列的形式對所掛接的資料庫操作了!


   Cmd 進入命令列
   1)
   建立資料庫檔案:
   >SQLite3 d:/test.db 回車
   就生成了一個test.db在d盤。
   這樣同時也SQLite3掛上了這個test.db

   2)
   用.help可以看看有什麼命令
   >.help 回車即可

   3)可以在這裡直接輸入SQL語句建立表格 用;結束,然後回車就可以看到了

   4)看看有建立了多少表
   >.tables

   5)看錶結構
   >.schema 表名

   6)看看目前掛的資料庫
   >.database

   7)如果要把查詢輸出到檔案
   >.output 檔名
   > 查詢語句;
   查詢結果就輸出到了檔案c:/query.txt
   把查詢結果用螢幕輸出
   >.output stdout


   8)把表結構輸出,同時索引也會輸出
     .dump 表名
   9)退出
   >.exit 或者.quit

sqlite官方幫助提示資訊:
sqlite> .help
.help
.bail ON|OFF           Stop after hitting an error.  Default OFF
.databases             List names and files of attached databases
.dump ?TABLE? ...      Dump the database in an SQL text format
.echo ON|OFF           Turn command echo on or off
.exit                  Exit this program
.explain ON|OFF        Turn output mode suitable for EXPLAIN on or off.
.header(s) ON|OFF      Turn display of headers on or off
.help                  Show this message
.import FILE TABLE     Import data from FILE into TABLE
.indices TABLE         Show names of all indices on TABLE
.load FILE ?ENTRY?     Load an extension library
.mode MODE ?TABLE?     Set output mode where MODE is one of:
                         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 .separator string
                         tabs     Tab-separated values
                         tcl      TCL list elements
.nullvalue STRING      Print STRING in place of NULL values
.output FILENAME       Send output to FILENAME
.output stdout         Send output to the screen
.prompt MAIN CONTINUE  Replace the standard prompts
.quit                  Exit this program
.read FILENAME         Execute SQL in FILENAME
.schema ?TABLE?        Show the CREATE statements
.separator STRING      Change separator used by output mode and .import
.show                  Show the current values for various settings
.tables ?PATTERN?      List names of tables matching a LIKE pattern
.timeout MS            Try opening locked tables for MS milliseconds
.timer ON|OFF          Turn the CPU timer measurement on or off
.width NUM NUM ...     Set column widths for "column" mode

它支援大部分的SQL命令,這是一個列表:

ATTACH DATABASE BEGIN TRANSACTION comment COMMIT TRANSACTION COPY CREATE INDEX CREATE TABLE CREATE TRIGGER CREATE VIEW DELETE DETACH DATABASE DROP INDEX DROP TABLE DROP TRIGGER DROP VIEW END TRANSACTION EXPLAIN expression INSERT ON CONFLICT clause PRAGMA REPLACE ROLLBACK TRANSACTION SELECT UPDATE

它還提供了虛擬機器用於處理sql語句,這是一個很有趣的東西。支援事務功能。