1. 程式人生 > >SQLite (一) - 簡介與安裝使用

SQLite (一) - 簡介與安裝使用

前言:在Android應用中有很多開源資料庫框架可以選擇使用(GreenDao、LitePal、Realm……),幫我們封裝的很好,效能也很好使用也很方便,大大的提高我們的開發效率,為什麼這次我會選擇從頭開始學一遍SQLite了,開源框架確實是很方便,使用多了時間久了,如果不進行適當鞏固基礎性的東西,我們會慢慢淡忘掉;也正好最近在做一個IM應用,是一個老專案(應該是14年啟動的專案),在即時通訊模組中,其中涉及到本地資料庫SQLite,使用SQLite進行聊天記錄本地快取,於是衝個機會重頭學一遍。

簡介

在我們使用一個東西的時候,最起碼要了解它的一些特徵和使用場景,才能合理的選用,關於SQLite的介紹,先看看

官網怎麼描述:

SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private. SQLite is the most widely deployed database in the world with more applications than we can count .


SQLite is an embedded SQL database engine. Unlike most other SQL databases, SQLite does not have a separate server process. SQLite reads and writes directly to ordinary disk files. A complete SQL database with multiple tables, indices, triggers, and views, is contained in a single disk file.

我英文不是很好,大概的意思就是,SQLite是一個程序內庫,它實現了自給, 無伺服器, 零配置, 事務性SQL資料庫引擎,程式碼開源,同時應用部署很廣泛的一種資料庫;SQLite是一個嵌入式SQL資料庫引擎,與大多數其他SQL資料庫不同,SQLite沒有單獨的伺服器程序,可以直接讀寫普通磁碟檔案,比直接檔案系統I/O更快。

既然是一種使用部署很廣泛嵌入式資料庫,使用場景也一定很豐富,官方介紹的使用場景,開場白就介紹了SQLite與其他資料庫的不同,其實就是介紹SQLite的差異化。

SQLite is not directly comparable to client/server SQL database engines such as MySQL, Oracle, PostgreSQL, or SQL Server since SQLite is trying to solve a different problem.


Client/server SQL database engines strive to implement a shared repository of enterprise data. They emphasize scalability, concurrency, centralization, and control. SQLite strives to provide local data storage for individual applications and devices. SQLite emphasizes economy, efficiency, reliability, independence, and simplicity.


SQLite does not compete with client/server databases. SQLite competes with fopen().

SQLite無法與客戶機/伺服器SQL資料庫引擎(如MySQL、Oracle、PostgreSQL或SQLServer)直接比較,也就是說不與客戶機/伺服器SQL資料庫引擎產生競爭關係,因為SQLite的專注的核心領域與其不同。

  • 嵌入式裝置和物聯網 :手機、機頂盒、電視機、遊戲機、照相機、手錶……
  • 應用檔案格式/檔案存檔:比如應用程式本地磁碟讀寫存取,效率效能比傳統IO更高
  • 中等流量的網站:官方介紹能承受100 K點選率/日,甚至更高,100K不是一個上限,可能是10倍以上。
  • ……………………

安裝

這裡主要介紹在Windows上安裝(64位機子)
1、進入官網下載sqlite-dll-win64-x64-3250300.zipsqlite-tools-win32-x86-3250300.zip這兩個檔案
在這裡插入圖片描述

2、把sqlite-dll-win64-x64-3250300.zipsqlite-tools-win32-x86-3250300.zip解壓後,我是把解壓檔案放在D盤下了。
在這裡插入圖片描述

3、最後使用cmd命令符檢查是否安裝成功,首頁進入D盤的sqlite資料夾,接著執行sqlite3命令符會顯示下面結果則已經成功了。
在這裡插入圖片描述

三步就可以完成sqlite資料庫的安裝。

建立資料庫

下面以建立userDB.db為例,使用命令符sqlite3.exe userDB.db建立即可,步驟如下:

1、開啟新命令提示符視窗,進入sqlite資料夾,執行sqlite3.exe userDB.db命令符,此userDB.db已經建立成功了,但此時在sqlite資料夾是看不見的。
在這裡插入圖片描述
注意:在執行sqlite3.exe userDB.db命令符,不要在sqlite3命令符下執行(就是不要在檢查是否安裝成功的cmd命令符下執行),不然會如下報錯Error: near "sqlite3": syntax error的,要把之前執行了sqlite3命令符的視窗關閉,重新開啟一個視窗,去執行建立資料庫的cmd命令。

2、在上面我們建立了資料庫,但是我們看不見,為了能在資料夾顯示出來,執行.databases命令符,會顯示資料庫所在檔案路徑。
在這裡插入圖片描述
在這裡插入圖片描述