Try   HackMD

SQLite使用紀錄

LHB阿好伯, 2020/04/26

tags: SQL

為何使用SQLite?

以下是爲什麼SQLite比其他輕量級數據庫受歡迎的功能列表:

SQLite是完全免費的:SQLite是開源的。 因此,不需要許可證就可以自由地使用它。
SQLite是無服務器的:SQLite不需要服務器進程或系統來操作。
SQLite非常靈活:它可以在同一個會話上同時處理多個數據庫。
SQLite不需要配置:SQLite無需設置或管理。
SQLite是一個跨平臺的數據庫系統:除了在大多數平臺,如Windows,Mac OS,Linux和Unix。 它也可以用於許多嵌入式操作系統,如Symbian,Android和Windows CE上使用。
存儲數據很容易:SQLite提供了一種有效的數據存儲方式。

下載與安裝

資料下載

在SQLite的官方網站,下載頁面Precompiled Binaries for Windows的三個檔案
放置於一個檔案中
例如這次我在C槽建立一個SQLite的資料夾
並將資料解壓縮

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

更改環境變數

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

sqlite-tools-win32-x86-3310100資料夾中sqlite3.exe檔案放置於C:\SQLite資料夾中

執行

開啟命令提示字元
輸入cd C:\SQLite

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

建立一個範例資料sqlite3 demo.db

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

.database 檢視資料庫
.open 連結資料庫
.tables 檢視資料庫中的表格
.header on 檢視表格時顯示變數名稱
.mode column 檢視表格時比較漂亮
.shell cls 清空 SQLite 命令列
.exit 離開 SQLite 命令列

讀取CSV檔

使用 .import 指令匯入 .csv 檔案建立表格

.mode csv .import nba_teams.csv nba_teams

重要的SQLite 點命令的列表.help

開啟sqlite3.exe,自動調出Windows命令列視窗

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

當SQLite命令列提示符出現時,輸入.help,將出現一列類似相關命令的說明
輸入.exit後退出程式
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

命令 描述
.backup ?DB? FILE 备份 DB 数据库(默认是 "main")到 FILE 文件。
.bail ON OFF
.databases 列出数据库的名称及其所依附的文件。
.dump ?TABLE? 以 SQL 文本格式转储数据库。如果指定了 TABLE 表,则只转储匹配 LIKE 模式的 TABLE 表。
.echo ON OFF
.exit 退出 SQLite 提示符。
.explain ON OFF
.header(s) ON OFF
.help 显示消息。
.import FILE TABLE 导入来自 FILE 文件的数据到 TABLE 表中。
.indices ?TABLE? 显示所有索引的名称。如果指定了 TABLE 表,则只显示匹配 LIKE 模式的 TABLE 表的索引。
.load FILE ?ENTRY? 加载一个扩展库。
.log FILE off
.mode MODE 设置输出模式,MODE 可以是下列之一: csv 逗号分隔的值, column 左对齐的列, html HTML<table> 代码, insert TABLE 表的 SQL 插入(insert)语句, line 每行一个值, list 由 .separator 字符串分隔的值, tabs 由 Tab 分隔的值, tcl TCL 列表元素
.nullvalue STRING 在 NULL 值的地方输出 STRING 字符串。
.output FILENAME 发送输出到 FILENAME 文件。
.output stdout 发送输出到屏幕。
.print STRING 逐字地输出 STRING 字符串。
.prompt MAIN CONTINUE 替换标准提示符。
.quit 退出 SQLite 提示符。
.read FILENAME 执行 FILENAME 文件中的 SQL。
.schema ?TABLE? 显示 CREATE 语句。如果指定了 TABLE 表,则只显示匹配 LIKE 模式的 TABLE 表。
.separator STRING 改变输出模式和 .import 所使用的分隔符。
.show 显示各种设置的当前值。
.stats ON OFF
.tables ?PATTERN? 列出匹配 LIKE 模式的表的名称。
.timeout MS 尝试打开锁定的表 MS 毫秒。
.width NUM NUM 为 "column" 模式设置列宽度。
.timer ON OFF

SQLite命令提示符的默認設置.show

.show命令,來查看SQLite命令提示符的默認設置

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

格式化輸出

.header on .mode column .timer on

SQLite數據庫瀏覽器_DB4S

建議安裝DB Browser for SQLite
可以更方便的操作SQLite

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

https://sqlitebrowser.org/

  • 創建並壓縮數據庫文件
  • 創建,定義,修改和刪除表
  • 創建,定義和刪除索引
  • 瀏覽,編輯,添加和刪除記錄
  • 搜索記錄
  • 以文本形式導入和導出記錄
  • 從CSV文件導入和導出表格
  • 從/到SQL轉儲文件導入和導出數據庫
  • 發出SQL查詢並檢查結果
  • 檢查應用程序發出的所有SQL命令的日誌
  • 根據表或查詢數據繪製簡單圖形

參考資料

https://www.1ju.org/sqlite/features

全文分享至

https://www.facebook.com/LHB0222/

有疑問想討論的都歡迎於下方留言

喜歡的幫我分享給所有的朋友 \o/

有所錯誤歡迎指教