欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報(bào)激活碼778899分享:一文搞定SQLite數(shù)據(jù)庫

柚子快報(bào)激活碼778899分享:一文搞定SQLite數(shù)據(jù)庫

http://yzkb.51969.com/

文章目錄

SQLite數(shù)據(jù)庫一、SQLite簡介1、簡介2、SQLite特性3、適用場景4、不適用場景5、如何選擇

二、SQLite安裝1、下載2、安裝

三、SQLite基本語法1、數(shù)據(jù)庫操作①、新建數(shù)據(jù)庫②、查看數(shù)據(jù)庫③、查看幫助指令

2、表操作①、新增表②、查看表信息③、查看表索引信息④、查看表結(jié)構(gòu)信息

2、數(shù)據(jù)記錄操作①、新增記錄②、查看記錄③、不同格式輸出

3、腳本文件操作①、從文件讀取sql并執(zhí)行②、輸出到文件

4、備份恢復(fù)操作①備份②恢復(fù)

四、SQLite應(yīng)用——go-sqlite3五、SQLite應(yīng)用——微信1、sqlitebrowser2、PyWxDump3、WeChatMsg

SQLite數(shù)據(jù)庫

相關(guān)鏈接:

SQLite官網(wǎng):https://www.sqlite.org/

SQLite安裝:https://sqlite.org/download.html

SQLite使用:https://sqlite.org/quickstart.html

SQLite語法:http://www.it028.com/sqlite-tutorial.html

SQLite源碼:https://github.com/sqlite/sqlite

一、SQLite簡介

1、簡介

SQLite 是一個(gè)用 C 語言編寫的開源、輕量級、快速、獨(dú)立且高可靠性的嵌入式關(guān)系型數(shù)據(jù)庫,它提供了功能齊全的數(shù)據(jù)庫解決方案。SQLite 幾乎在所有的手機(jī)和計(jì)算機(jī)上運(yùn)行,它被嵌入到人們每天都在使用的眾多應(yīng)用程序中。 這也是世界上裝機(jī)量最大的數(shù)據(jù)庫SQLite,根據(jù)SQLite的官方統(tǒng)計(jì),目前正在使用的SQLite數(shù)據(jù)庫實(shí)例已經(jīng)超過了1萬億個(gè)。

此外,SQLite 還具有穩(wěn)定的文件格式、跨平臺能力和向后兼容性等特點(diǎn)。SQLite 的開發(fā)者承諾,至少在 2050 年之前保持該文件格式不變。

SQLite 可以在不需要的服務(wù)器的情況下,直接嵌入到一些物聯(lián)網(wǎng)設(shè)備, 比如家電,路由器。汽車等作為本地?cái)?shù)據(jù)庫使用。智能手機(jī)也正好契合 SQLite 的使用場景。為什么說手機(jī)上肯定有 SQLite 呢?因?yàn)?SQLite 會(huì)隨著應(yīng)用程序代碼一起打包,每安裝一個(gè)APP都有可能部署一個(gè)SQLite的數(shù)據(jù)庫。

當(dāng)然了,SQLite 不能直接與諸如 MySQL、Oracle、PostgreSQL 或 SQL Server 之類的客戶端/服務(wù)器 SQL 數(shù)據(jù)庫引擎相比較,因?yàn)?SQLite 試圖解決的是一個(gè)完全不同的場景問題。

客戶端/服務(wù)器 SQL 數(shù)據(jù)庫引擎努力實(shí)現(xiàn)企業(yè)數(shù)據(jù)的共享存儲(chǔ)庫。它們強(qiáng)調(diào)可擴(kuò)展性、并發(fā)性、集中化和控制??蛻舳?SQL 數(shù)據(jù)庫——SQLite 努力為單個(gè)應(yīng)用程序和設(shè)備提供本地?cái)?shù)據(jù)存儲(chǔ)。它強(qiáng)調(diào)經(jīng)濟(jì)性、效率、可靠性、獨(dú)立性和簡潔性。

2、SQLite特性

關(guān)于SQLite:https://sqlite.org/about.html

不要被名字中的“Lite”所誤導(dǎo)。SQLite 擁有功能齊全的 SQL 實(shí)現(xiàn),包括:

數(shù)量不限的表、索引、觸發(fā)器和視圖一張表中最多 32K 列,行數(shù)不限多列索引索引可以使用 DESC 和 COLLATE部分索引表達(dá)式上的索引聚簇索引覆蓋索引CHECK、UNIQUE、NOT NULL 和 FOREIGN KEY 約束。使用 BEGIN、COMMIT 和 ROLLBACK 的 ACID 事務(wù)使用 SAVEPOINT、RELEASE 和 ROLLBACK TO 的嵌套事務(wù)子查詢,包括相關(guān)子查詢LEFT、RIGHT 和 FULL OUTER JOINDISTINCT、ORDER BY、GROUP BY、HAVING、LIMIT 和 OFFSETUNION、UNION ALL、INTERSECT 和 EXCEPT豐富的標(biāo)準(zhǔn) SQL 函數(shù)庫包括 DISTINCT 聚合的聚合函數(shù)UPDATE、DELETE 和 INSERT包括遞歸公共表表達(dá)式的公共表表達(dá)式全文搜索R 樹索引JSON 支持REPLACE INTOREINDEXGLOB 操作符ON CONFLICT 子句INDEXED BY 子句虛擬表使用 ATTACH DATABASE 在同一個(gè)數(shù)據(jù)庫連接上的多個(gè)數(shù)據(jù)庫

3、適用場景

物聯(lián)網(wǎng)數(shù)據(jù)庫:SQLite 非常適合用于嵌入式設(shè)備:手機(jī)、機(jī)頂盒、電視、游戲機(jī)、相機(jī)、手表、廚房電器、恒溫器、汽車、機(jī)床、飛機(jī)、遠(yuǎn)程傳感器、無人機(jī)、醫(yī)療設(shè)備和機(jī)器人等,即“物聯(lián)網(wǎng)”。SQLite 代碼占用空間小,能高效利用內(nèi)存、磁盤空間和磁盤帶寬,可靠性高,并且不需要數(shù)據(jù)庫管理員進(jìn)行維護(hù)。

應(yīng)用文件格式:SQLite 數(shù)據(jù)庫支持將 XML、JSON、CSV 或某些專有的格式寫入到應(yīng)用程序所使用的磁盤文件中,并且不需要編寫和調(diào)試解析器,這樣數(shù)據(jù)也更容易實(shí)現(xiàn)跨平臺的訪問,并且SQLite 數(shù)據(jù)庫支持事務(wù)。 SQLite 經(jīng)常被用作桌面應(yīng)用程序(如版本控制系統(tǒng)、財(cái)務(wù)分析工具、媒體編目和編輯套件、CAD 軟件包、記錄保存程序等)的磁盤文件格式。傳統(tǒng)的“文件/打開”操作會(huì)調(diào)用 sqlite3_open() 來連接到數(shù)據(jù)庫文件。這種方法有許多好處,包括提高性能、降低成本和復(fù)雜性以及提高可靠性。

網(wǎng)站數(shù)據(jù)庫:SQLite 不需要配置或調(diào)優(yōu),并且可以將信息存儲(chǔ)在普通的磁盤文件中,這也使其成為中小型網(wǎng)站數(shù)據(jù)庫的熱門選擇。SQLite 作為大多數(shù)中低流量網(wǎng)站(每天點(diǎn)擊量在10W次甚至100W次)的數(shù)據(jù)庫引擎效果很好。

企業(yè)關(guān)系型數(shù)據(jù)庫管理系統(tǒng)的替代品:SQLite 經(jīng)常被用作企業(yè)關(guān)系型數(shù)據(jù)庫管理系統(tǒng)的替代品,用于演示或測試。SQLite 速度快且無需設(shè)置,這大大減少了測試的麻煩,使演示更加活潑且易于啟動(dòng)。

數(shù)據(jù)分析:懂 SQL 的人可以使用 sqlite3 命令行外殼(或各種第三方 SQLite 訪問程序)來分析大型數(shù)據(jù)集。原始數(shù)據(jù)可以從 CSV 文件導(dǎo)入,然后對這些數(shù)據(jù)進(jìn)行切片和切塊以生成無數(shù)的匯總報(bào)告。更復(fù)雜的分析可以使用 Python(內(nèi)置了 SQLite)編寫的簡單腳本,或者使用 R 或其他語言的現(xiàn)成適配器來完成。可能的用途包括網(wǎng)站日志分析、體育統(tǒng)計(jì)分析、編程指標(biāo)的編譯和實(shí)驗(yàn)結(jié)果的分析。許多生物信息學(xué)研究人員以這種方式使用 SQLite。

當(dāng)然,使用企業(yè)客戶端/服務(wù)器數(shù)據(jù)庫也可以完成同樣的事情。SQLite 的優(yōu)點(diǎn)是更易于安裝和使用,并且生成的數(shù)據(jù)庫是一個(gè)可以寫入 USB 記憶棒或通過電子郵件發(fā)送給同事的單個(gè)文件。

企業(yè)數(shù)據(jù)緩存:許多應(yīng)用程序使用 SQLite 作為企業(yè) RDBMS 相關(guān)內(nèi)容的緩存。這減少了延遲,因?yàn)楝F(xiàn)在大多數(shù)查詢都針對本地緩存進(jìn)行,避免了網(wǎng)絡(luò)往返。它還減少了網(wǎng)絡(luò)和中央數(shù)據(jù)庫服務(wù)器的負(fù)載。在許多情況下,這意味著客戶端應(yīng)用程序在網(wǎng)絡(luò)中斷期間仍能繼續(xù)運(yùn)行。

替代臨時(shí)磁盤文件:許多程序使用 fopen()、fread() 和 fwrite() 以自制格式創(chuàng)建和管理數(shù)據(jù)文件。SQLite 作為這些臨時(shí)數(shù)據(jù)文件的替代品效果特別好。與直覺相反,SQLite 在讀寫磁盤內(nèi)容方面可能比文件系統(tǒng)更快。

內(nèi)部或臨時(shí)數(shù)據(jù)庫:對于有大量數(shù)據(jù)需要以各種方式篩選和排序的程序,通常將數(shù)據(jù)加載到內(nèi)存中的 SQLite 數(shù)據(jù)庫中,并使用帶有連接和 ORDER BY 子句的查詢以所需的形式和順序提取數(shù)據(jù),比嘗試手動(dòng)編碼相同的操作更容易和更快。以這種方式在內(nèi)部使用 SQL 數(shù)據(jù)庫還為程序提供了更大的靈活性,因?yàn)榭梢蕴砑有碌牧泻退饕鵁o需重新編碼每個(gè)查詢。

教育和培訓(xùn):由于設(shè)置和使用簡單(安裝很簡單:只需將 sqlite3 或 sqlite3.exe 可執(zhí)行文件復(fù)制到目標(biāo)機(jī)器并運(yùn)行它),SQLite 是用于教授 SQL 的良好數(shù)據(jù)庫引擎。學(xué)生可以輕松創(chuàng)建任意數(shù)量的數(shù)據(jù)庫,并可以通過電子郵件將數(shù)據(jù)庫發(fā)送給教師進(jìn)行評論或評分。對于有興趣研究 RDBMS 如何實(shí)現(xiàn)的更高級學(xué)生,模塊化且注釋良好、文檔齊全的 SQLite 代碼可以作為一個(gè)良好的基礎(chǔ)。

4、不適用場景

客戶端/服務(wù)器應(yīng)用程序:如果有許多客戶端程序通過網(wǎng)絡(luò)向同一個(gè)數(shù)據(jù)庫發(fā)送 SQL,那么應(yīng)使用客戶端/服務(wù)器數(shù)據(jù)庫引擎而不是 SQLite。SQLite 可以在網(wǎng)絡(luò)文件系統(tǒng)上運(yùn)行,但由于大多數(shù)網(wǎng)絡(luò)文件系統(tǒng)存在延遲,性能不會(huì)太好。此外,許多網(wǎng)絡(luò)文件系統(tǒng)實(shí)現(xiàn)(在 Unix 和 Windows 上)中的文件鎖定邏輯都存在錯(cuò)誤。如果文件鎖定不能正常工作,兩個(gè)或更多的客戶端可能會(huì)嘗試同時(shí)修改同一個(gè)數(shù)據(jù)庫的同一部分,從而導(dǎo)致?lián)p壞。由于這個(gè)問題源于底層文件系統(tǒng)實(shí)現(xiàn)中的錯(cuò)誤,SQLite 對此無能為力。

一個(gè)好的經(jīng)驗(yàn)法則是,在同一數(shù)據(jù)庫將被直接訪問(沒有中間應(yīng)用服務(wù)器)并且同時(shí)通過網(wǎng)絡(luò)從多臺計(jì)算機(jī)訪問的情況下,避免使用 SQLite。

高流量網(wǎng)站:SQLite 通常作為網(wǎng)站的數(shù)據(jù)庫后端運(yùn)行良好。但如果網(wǎng)站寫入密集或非常繁忙以至于需要多個(gè)服務(wù)器,那么應(yīng)考慮使用企業(yè)級的客戶端/服務(wù)器數(shù)據(jù)庫引擎而不是 SQLite。

非常大的數(shù)據(jù)集:SQLite 數(shù)據(jù)庫的大小限制為 281 太字節(jié)(2^48 字節(jié),256 太字節(jié))。即使它能夠處理更大的數(shù)據(jù)庫,SQLite 也將整個(gè)數(shù)據(jù)庫存儲(chǔ)在一個(gè)磁盤文件中,而許多文件系統(tǒng)將文件的最大大小限制在小于此的值。因此,如果您正在考慮如此規(guī)模的數(shù)據(jù)庫,最好考慮使用將其內(nèi)容分布在多個(gè)磁盤文件中,甚至可能分布在多個(gè)卷中的客戶端/服務(wù)器數(shù)據(jù)庫引擎。

高并發(fā):SQLite 支持無限數(shù)量的同時(shí)讀取者,但在任何時(shí)刻都只允許一個(gè)寫入者。對于許多情況,這不是問題。寫入者會(huì)排隊(duì)。每個(gè)應(yīng)用程序快速完成其數(shù)據(jù)庫工作并繼續(xù),并且任何鎖定都不會(huì)持續(xù)超過幾十毫秒。但有些應(yīng)用程序需要更高的并發(fā),這些應(yīng)用程序可能需要尋求其他解決方案。

5、如何選擇

數(shù)據(jù)是否通過網(wǎng)絡(luò)與應(yīng)用程序分離? → 選擇客戶端/服務(wù)器

關(guān)系型數(shù)據(jù)庫引擎充當(dāng)降低帶寬的數(shù)據(jù)過濾器。因此,最好將數(shù)據(jù)庫引擎和數(shù)據(jù)放在同一物理設(shè)備上,這樣高帶寬的引擎到磁盤的鏈接就不必穿越網(wǎng)絡(luò),只有低帶寬的應(yīng)用程序到引擎的鏈接需要穿越網(wǎng)絡(luò)。

但 SQLite 是內(nèi)置于應(yīng)用程序中的。因此,如果數(shù)據(jù)與應(yīng)用程序在不同的設(shè)備上,就需要高帶寬的引擎到磁盤的鏈接通過網(wǎng)絡(luò)。這可行,但并非最優(yōu)。因此,當(dāng)數(shù)據(jù)與應(yīng)用程序在不同的設(shè)備上時(shí),通常最好選擇客戶端/服務(wù)器數(shù)據(jù)庫引擎。

注意:在這條規(guī)則中,“應(yīng)用程序”是指發(fā)出 SQL 語句的代碼。如果“應(yīng)用程序”是一個(gè)應(yīng)用服務(wù)器,并且內(nèi)容與應(yīng)用服務(wù)器位于同一物理機(jī)器上,那么即使最終用戶在另一個(gè)網(wǎng)絡(luò)跳點(diǎn)之外,SQLite 可能仍然適用。

有許多并發(fā)寫入者嗎? → 選擇客戶端/服務(wù)器

如果許多線程和/或進(jìn)程需要在同一時(shí)刻寫入數(shù)據(jù)庫(并且它們不能排隊(duì)輪流),那么最好選擇支持該功能的數(shù)據(jù)庫引擎,這通常意味著選擇客戶端/服務(wù)器數(shù)據(jù)庫引擎。

SQLite 每個(gè)數(shù)據(jù)庫文件在同一時(shí)間只支持一個(gè)寫入者。但在大多數(shù)情況下,一個(gè)寫事務(wù)只需要幾毫秒,因此多個(gè)寫入者可以簡單地輪流進(jìn)行。SQLite 處理的寫并發(fā)比許多人想象的要多。然而,客戶端/服務(wù)器數(shù)據(jù)庫系統(tǒng),由于它們有一個(gè)長期運(yùn)行的服務(wù)器進(jìn)程來協(xié)調(diào)訪問,通常能夠處理比 SQLite 多得多的寫并發(fā)。

大數(shù)據(jù)? → 選擇客戶端/服務(wù)器

如果您的數(shù)據(jù)將增長到您無法或不愿意放入單個(gè)磁盤文件的大小,那么您應(yīng)該選擇除 SQLite 之外的解決方案。SQLite 支持最大 281 太字節(jié)大小的數(shù)據(jù)庫,假設(shè)您能找到支持 281 太字節(jié)文件的磁盤驅(qū)動(dòng)器和文件系統(tǒng)。即便如此,當(dāng)內(nèi)容的大小看起來可能會(huì)逐漸進(jìn)入太字節(jié)范圍時(shí),最好考慮使用集中式的客戶端/服務(wù)器數(shù)據(jù)庫。

否則 → 選擇 SQLite!

對于具有低寫入者并發(fā)和少于太字節(jié)內(nèi)容的設(shè)備本地存儲(chǔ),SQLite 幾乎總是一個(gè)更好的解決方案。SQLite 快速、可靠,并且不需要配置或維護(hù)。它使事情變得簡單。SQLite“就是能行”。

二、SQLite安裝

1、下載

在 SQLite 官方頁面下載適合操作系統(tǒng)的壓縮包。

下載并解壓,并配置環(huán)境變量后,無論是在 Windows、Linux 還是 Mac OS 系統(tǒng)上,都可以得到一個(gè) sqlite3 命令行工具。

2、安裝

以Linux系統(tǒng)安裝為例

# 創(chuàng)建sqlite目錄,并上傳文件到sqlite目錄下

[root@k8s-dev ~]# mkdir sqlite

[root@k8s-dev ~]# cd sqlite/

[root@k8s-dev sqlite]# ll

total 10792

-rw-r--r-- 1 root root 11048231 Aug 15 14:44 sqlite-tools-linux-x64-3460100.zip

[root@k8s-dev sqlite]# unzip sqlite-tools-linux-x64-3460100.zip

Archive: sqlite-tools-linux-x64-3460100.zip

inflating: sqlite3

inflating: sqldiff

inflating: sqlite3_analyzer

[root@k8s-dev sqlite]# ll

total 33368

-rwxrwxr-x 1 root root 6946064 Aug 13 19:05 sqldiff

-rwxrwxr-x 1 root root 8710792 Aug 13 19:04 sqlite3

-rwxrwxr-x 1 root root 7454784 Aug 13 19:05 sqlite3_analyzer

-rw-r--r-- 1 root root 11048231 Aug 15 14:44 sqlite-tools-linux-x64-3460100.zip

# 當(dāng)前未配置環(huán)境變量

[root@k8s-dev sqlite]# ./sqlite3 test.db

SQLite version 3.7.17 2013-05-20 00:56:22

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite>

配置環(huán)境變量

vim /etc/profile

## 在profile文件最后一行添加上如下內(nèi)容

## sqlite安裝路徑

export PATH=$PATH:/root/sqlite

# 讓環(huán)境變量生效

source /etc/profile

# 再次執(zhí)行打開一個(gè)sqlite數(shù)據(jù)庫

[root@k8s-dev sqlite]# sqlite3 test.db

SQLite version 3.7.17 2013-05-20 00:56:22

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite>

三、SQLite基本語法

SQLite語法:http://www.it028.com/sqlite-tutorial.html

SQLite支持SQL語法,如果你會(huì)MySQL,那么SQLite語法的學(xué)習(xí)也就變得輕而易舉。

1、數(shù)據(jù)庫操作

①、新建數(shù)據(jù)庫

可以直接執(zhí)行 sqlite3 filename 打開或創(chuàng)建一個(gè) SQLite 數(shù)據(jù)庫。如果文件不存在,SQLite 會(huì)自動(dòng)創(chuàng)建它。

# 再次執(zhí)行打開一個(gè)sqlite數(shù)據(jù)庫

[root@k8s-dev sqlite]# sqlite3 test.db

SQLite version 3.7.17 2013-05-20 00:56:22

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite>

也可以首先創(chuàng)建一個(gè)空白文件,然后使用 sqlite3 命令打開它。

②、查看數(shù)據(jù)庫

sqlite> .database

seq name file

--- --------------- ----------------------------------------------------------

0 main /root/sqlite/test.db

使用 .exit 退出 sqlite3 工具。 再查看當(dāng)前目錄,會(huì)發(fā)現(xiàn)新增了一個(gè) test.db 文件。

sqlite> .exit

[root@k8s-dev sqlite]# ll

total 33372

-rwxrwxr-x 1 root root 6946064 Aug 13 19:05 sqldiff

-rwxrwxr-x 1 root root 8710792 Aug 13 19:04 sqlite3

-rwxrwxr-x 1 root root 7454784 Aug 13 19:05 sqlite3_analyzer

-rw-r--r-- 1 root root 11048231 Aug 15 14:44 sqlite-tools-linux-x64-3460100.zip

-rw-r--r-- 1 root root 2048 Aug 15 14:46 test.db

③、查看幫助指令

使用 .help 查看 幫助信息?!局匾?/p>

sqlite> .help

.backup ?DB? FILE Backup DB (default "main") to FILE

.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

If TABLE specified, only dump tables matching

LIKE pattern TABLE.

.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.

With no args, it turns EXPLAIN on.

.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

If TABLE specified, only show indices for tables

matching LIKE pattern TABLE.

.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:

csv Comma-separated values

column Left-aligned columns. (See .width)

html HTML

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 Use STRING in place of NULL values

.output FILENAME Send output to FILENAME

.output stdout Send output to the screen

.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

.schema ?TABLE? Show the CREATE statements

If TABLE specified, only show tables matching

LIKE pattern TABLE.

.separator STRING Change separator used by output mode and .import

.show Show the current values for various settings

.stats ON|OFF Turn stats on or off

.tables ?TABLE? List names of tables

If TABLE specified, only list tables matching

LIKE pattern TABLE.

.timeout MS Try opening locked tables for MS milliseconds

.trace FILE|off Output each SQL statement as it is run

.vfsname ?AUX? Print the name of the VFS stack

.width NUM1 NUM2 ... Set column widths for "column" mode

.timer ON|OFF Turn the CPU timer measurement on or off

2、表操作

①、新增表

使用 CREATE TABLE 命令創(chuàng)建一個(gè)名為 user 的表。

CREATE TABLE user(name text,age int);

CREATE TABLE student(id int,name text,age int,primary key(id));

②、查看表信息

使用 .tables 或者.table tableName命令查看現(xiàn)有表格

sqlite> .tables

student user

sqlite> .tables user

user

sqlite> .table user

user

③、查看表索引信息

## user表沒有建索引

sqlite> .indices user

sqlite> .indices student

sqlite_autoindex_student_1

④、查看表結(jié)構(gòu)信息

sqlite> .schema

CREATE TABLE user(name text,age int);

CREATE TABLE student(id int,name text,age int,primary key(id));

sqlite> .schema student

CREATE TABLE student(id int,name text,age int,primary key(id));

2、數(shù)據(jù)記錄操作

①、新增記錄

sqlite> insert into user values('Tom',20);

sqlite> insert into user values('Jerry',18);

sqlite> insert into user values('Jack',25);

②、查看記錄

sqlite> select * from user;

Tom|20

Jerry|18

Jack|25

③、不同格式輸出

qlite> .mode help

Error: mode should be one of: column csv html insert line list tabs tcl

qlite> .mode column

sqlite> select * from user;

Tom 20

Jerry 18

Jack 25

sqlite> .mode html

sqlite> select * from user;

sqlite> .mode tabs

sqlite> select * from user;

Tom 20

Jerry 18

Jack 25

qlite> .mode csv

sqlite> select * from user;

Tom,20

Jerry,18

Jack,25

3、腳本文件操作

①、從文件讀取sql并執(zhí)行

sqlite> .quit

[root@k8s-dev sqlite]# echo "select * from user;" > user.sql

[root@k8s-dev sqlite]# sqlite3 test.db

SQLite version 3.7.17 2013-05-20 00:56:22

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite> .read user.sql

Tom|20

Jerry|18

Jack|25

②、輸出到文件

sqlite> .output user.csv

sqlite> .mode csv

sqlite> select * from user;

sqlite> .quit

[root@k8s-dev sqlite]# ll

total 33380

-rwxrwxr-x 1 root root 6946064 Aug 13 19:05 sqldiff

-rwxrwxr-x 1 root root 8710792 Aug 13 19:04 sqlite3

-rwxrwxr-x 1 root root 7454784 Aug 13 19:05 sqlite3_analyzer

-rw-r--r-- 1 root root 11048231 Aug 15 14:44 sqlite-tools-linux-x64-3460100.zip

-rw-r--r-- 1 root root 2048 Aug 15 14:53 test.db

-rw-r--r-- 1 root root 72 Aug 15 14:59 user.csv

-rw-r--r-- 1 root root 19 Aug 15 14:58 user.sql

[root@k8s-dev sqlite]# cat user.csv

Tom,20

Jerry,18

Jack,25

4、備份恢復(fù)操作

在涉及數(shù)據(jù)庫操作時(shí),備份和恢復(fù)是至關(guān)重要的步驟,它們用于防止數(shù)據(jù)丟失并確保數(shù)據(jù)的持續(xù)性。SQLite 提供了簡單的方法來備份和恢復(fù)數(shù)據(jù)庫。

①備份

在 SQLite 中可以通過導(dǎo)出整個(gè)數(shù)據(jù)庫為一個(gè) SQL 腳本來備份數(shù)據(jù)庫。此功能使用 .dump 命令實(shí)現(xiàn)。

sqlite> .output backup-restore.sql

sqlite> .dump

sqlite> .quit

[root@k8s-dev sqlite]# ll

total 33392

-rw-r--r-- 1 root root 310 Aug 15 15:03 backup-restore.sql

-rwxrwxr-x 1 root root 6946064 Aug 13 19:05 sqldiff

-rwxrwxr-x 1 root root 8710792 Aug 13 19:04 sqlite3

-rwxrwxr-x 1 root root 7454784 Aug 13 19:05 sqlite3_analyzer

-rw-r--r-- 1 root root 11048231 Aug 15 14:44 sqlite-tools-linux-x64-3460100.zip

-rw-r--r-- 1 root root 4096 Aug 15 16:23 test.db

-rw-r--r-- 1 root root 108 Aug 15 15:00 user.csv

-rw-r--r-- 1 root root 20 Aug 15 15:00 user.sql

[root@k8s-dev sqlite]# cat backup-restore.sql

PRAGMA foreign_keys=OFF;

BEGIN TRANSACTION;

CREATE TABLE user(name text,age int);

INSERT INTO "user" VALUES('Tom',20);

INSERT INTO "user" VALUES('Jerry',18);

INSERT INTO "user" VALUES('Jack',25);

CREATE TABLE student(id int,name text,age int,primary key(id));

COMMIT;

②恢復(fù)

將上述備份出來的數(shù)據(jù),恢復(fù)到一個(gè)新的數(shù)據(jù)庫test-backup-restore.db上

[root@k8s-dev sqlite]# sqlite3 test-backup-restore.db

SQLite version 3.7.17 2013-05-20 00:56:22

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite> .tables

sqlite> .read backup-restore.sql

sqlite> .tables

student user

sqlite> select * from user;

Tom|20

Jerry|18

Jack|25

四、SQLite應(yīng)用——go-sqlite3

相關(guān)鏈接:https://github.com/mattn/go-sqlite3

Golang應(yīng)用SQLite代碼示例

### 新建一個(gè)go-sqlite項(xiàng)目

[root@k8s-dev ~]# mkdir go-sqlite

[root@k8s-dev ~]# cd go-sqlite

[root@k8s-dev go-sqlite]# go mod init go-sqlie-example

go: creating new go.mod: module go-sqlie

go: to add module requirements and sums:

go mod tidy

[root@k8s-dev go-sqlite]# go get github.com/mattn/go-sqlite3

[root@k8s-dev go-sqlite]# vim main.go

# 代碼具體如下:

[root@k8s-dev go-sqlite]# cat main.go

package main

import (

"database/sql"

"fmt"

_ "github.com/mattn/go-sqlite3"

"log"

"os"

)

func main() {

os.Remove("./foo.db")

## 新建數(shù)據(jù)庫

db, err := sql.Open("sqlite3", "./foo.db")

if err != nil {

log.Fatal(err)

}

defer db.Close()

sqlStmt := `

create table foo (id integer not null primary key, name text);

delete from foo;

`

## 新增表

_, err = db.Exec(sqlStmt)

if err != nil {

log.Printf("%q: %s\n", err, sqlStmt)

return

}

## 開啟事務(wù)

tx, err := db.Begin()

if err != nil {

log.Fatal(err)

}

stmt, err := tx.Prepare("insert into foo(id, name) values(?, ?)")

if err != nil {

log.Fatal(err)

}

defer stmt.Close()

for i := 0; i < 100; i++ {

## 新增記錄

_, err = stmt.Exec(i, fmt.Sprintf("hello world_%03d", i))

if err != nil {

log.Fatal(err)

}

}

## 提交事務(wù)

err = tx.Commit()

if err != nil {

log.Fatal(err)

}

## 查看記錄

rows, err := db.Query("select id, name from foo")

if err != nil {

log.Fatal(err)

}

defer rows.Close()

for rows.Next() {

var id int

var name string

err = rows.Scan(&id, &name)

if err != nil {

log.Fatal(err)

}

fmt.Println(id, name)

}

err = rows.Err()

if err != nil {

log.Fatal(err)

}

stmt, err = db.Prepare("select name from foo where id = ?")

if err != nil {

log.Fatal(err)

}

defer stmt.Close()

var name string

## 查看第3條記錄

err = stmt.QueryRow("3").Scan(&name)

if err != nil {

log.Fatal(err)

}

fmt.Println(name)

## 刪除所有記錄

_, err = db.Exec("delete from foo")

if err != nil {

log.Fatal(err)

}

## 新增3條記錄

_, err = db.Exec("insert into foo(id, name) values(1, 'foo'), (2, 'bar'), (3, 'baz')")

if err != nil {

log.Fatal(err)

}

## 查看所有記錄

rows, err = db.Query("select id, name from foo")

if err != nil {

log.Fatal(err)

}

defer rows.Close()

for rows.Next() {

var id int

var name string

err = rows.Scan(&id, &name)

if err != nil {

log.Fatal(err)

}

fmt.Println(id, name)

}

err = rows.Err()

if err != nil {

log.Fatal(err)

}

}

五、SQLite應(yīng)用——微信

SQLite也是微信的客戶端數(shù)據(jù)庫,里面保存了包括聊天記錄、聯(lián)系人、設(shè)置等關(guān)于微信的幾乎一切數(shù)據(jù)。

1、sqlitebrowser

SQLite可視化工具:https://github.com/sqlitebrowser/sqlitebrowser

下載之后解壓,新建或者直接打開一個(gè)SQLite db文件即可完成SQLite數(shù)據(jù)的可視化操作。

2、PyWxDump

微信數(shù)據(jù)信息查看工具:https://github.com/xaoyaoo/PyWxDump

下載wxdump.exe文件之后,雙擊運(yùn)行即可。該程序會(huì)自動(dòng)掃描微信程序,并將微信數(shù)據(jù)導(dǎo)出到wxdump_work目錄下,里面有個(gè)merge_all.db文件,該文件保存了微信的所有數(shù)據(jù)信息。用可視化工具sqlitebrowser將其打開如下所示,一共有48張表。

這里列舉幾個(gè)我們平時(shí)比較常用的表信息如下: 詳見GitHub 項(xiàng)目PyWxDump的doc目錄下的wx數(shù)據(jù)庫簡述.md文檔。

APPInfo:記錄使用微信進(jìn)行第三方登錄的信息 ChatCRMsg:記錄企業(yè)微信的信息 ChatRoom/ChatRoomInfo:記錄微信群信息 Contact:記錄所有聯(lián)系人的信息 ContactLabel:記錄聯(lián)系人標(biāo)簽信息 FavItems:記錄了微信收藏記錄信息 FavTagDatas:記錄收藏的標(biāo)簽信息 MSG:記錄了微信的聊天信息,最核心的信息 Session:記錄了所有會(huì)話信息 OpenIM 前綴:企業(yè)微信聊天記錄數(shù)據(jù),包括所有和企業(yè)微信聊天的數(shù)據(jù)。

3、WeChatMsg

微信數(shù)據(jù)信息導(dǎo)出工具:https://github.com/LC044/WeChatMsg

下載打包好的exe可執(zhí)行文件,雙擊即可運(yùn)行。

柚子快報(bào)激活碼778899分享:一文搞定SQLite數(shù)據(jù)庫

http://yzkb.51969.com/

好文鏈接

評論可見,查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。

轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/19555383.html

發(fā)布評論

您暫未設(shè)置收款碼

請?jiān)谥黝}配置——文章設(shè)置里上傳

掃描二維碼手機(jī)訪問

文章目錄

      Tom20
      Jerry18
      Jack25