一、什么是 SQLite 資料庫
SQLite 是嵌入式SQL資料庫引擎,與大多數其他 SQL 資料庫不同,SQLite 沒有單獨的服務器行程,SQLite 直接讀取和寫入普通磁盤檔案,具有多個表,索引,觸發器和視圖的完整 SQL 資料庫包含在單個磁盤檔案中,資料庫檔案格式是跨平臺的-您可以在32位和64位系統之間或在big-endian和 little-endian 體系結構之間自由復制資料庫 ,這些功能使SQLite成為應用程式檔案格式的流行選擇,
它還是一個本地資料庫,在本地生成一個資料庫檔案,不需要借助網路就能訪問,
二、SQLite 的下載安裝
官網下載地址:https://www.sqlite.org/download.html

找到你所對應的版本,我這里以 windows 為例,下載 sqlite-tools-win32-x86-3390400.zip 和 sqlite-dll-win64-x64-3390400.zip 兩個檔案,將檔案解壓放置單獨的檔案夾,解壓后一共是 5 個檔案

然后在電腦的環境變數中去配置這個檔案夾的路徑

到這一步,安裝和配置已經完成了,接下來就要驗證是否安裝成功了,打開 CMD 命令版,輸入 sqlite3,出現版本資訊,則表示安裝成功
如果輸入出現提示 sqlite3 不是內部命令,那就是環境變數沒有生效,配置環境變數后要重新啟動電腦才能生效

三、SQLite在程式中使用
這里以 .Net Framework 的專案為例演示
1、參考 SQLite 相關的包即可,NuGet 搜索 System.Data.SQLite ,點擊安裝,會自動下載所依賴的其他包
注意:不要在官網下載 System.Data.SQLite.dll ,如果你在官網下載這個 dll ,直接添加參考,運行代碼時會報錯 ,無法加載“DLL “SQLite.Interop.dll”:找不帶指定模塊”,很難解決

2、新建一個文本將檔案擴展名改為 db ,這樣我們就能本地訪問這個資料庫

3、訪問查看 SQLite 資料庫,有多種方式
可以使用官方提供的工具 SQLiteStudio :https://sqlitestudio.pl/
也可以使用其他的資料庫鏈接工具(這里使用的是 Navicat),如下,選擇連接資料庫的型別為 SQLite,添加鏈接,選擇現有的資料庫檔案就行,不需要賬號和密碼

4、寫 SQLite 幫助類
我這里寫的比較簡單,只有 3 個方法
因為 SQLite 可以本地讀取的資料庫,所以資料庫鏈接要寫成 絕對路徑,如:C:\資料庫\xx.db
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SQLite; 5 using System.Linq; 6 using System.Web; 7 8 namespace WebApplication1.DBhelper 9 { 10 public static class SqliteHelper 11 { 12 /// <summary> 13 /// 獲取連接字串 14 /// </summary> 15 /// <returns>連接字串</returns> 16 public static string GetSqlConnectionString() 17 { 18 //return ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; 19 return "data source=C:\\TestProject\\DB\\sqliteDb.db"; //xx.db 用絕對路徑 20 } 21 22 /// <summary> 23 /// 執行查詢陳述句,回傳DataSet 24 /// </summary> 25 /// <param name="SQLString">查詢陳述句</param> 26 /// <returns>DataSet</returns> 27 public static DataSet GetDataSet(string SQLString) 28 { 29 using (SQLiteConnection connection = new SQLiteConnection(GetSqlConnectionString())) 30 { 31 DataSet ds = new DataSet(); 32 try 33 { 34 connection.Open(); 35 SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection); 36 command.Fill(ds, "ds"); 37 } 38 catch (System.Data.SQLite.SQLiteException ex) 39 { 40 throw new Exception(ex.Message); 41 } 42 return ds; 43 } 44 } 45 46 /// <summary> 47 /// 執行查詢陳述句,回傳DataTable 48 /// </summary> 49 /// <param name="SQLString"></param> 50 /// <returns></returns> 51 /// <exception cref="Exception"></exception> 52 public static DataTable GetDataTable(string SQLString) 53 { 54 using (SQLiteConnection connection = new SQLiteConnection(GetSqlConnectionString())) 55 { 56 DataTable dt = new DataTable(); 57 try 58 { 59 connection.Open(); 60 SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection); 61 command.Fill(dt); 62 } 63 catch (System.Data.SQLite.SQLiteException ex) 64 { 65 throw new Exception(ex.Message); 66 } 67 return dt; 68 } 69 } 70 71 /// <summary> 72 /// 執行操作陳述句,回傳成功與否 73 /// </summary> 74 /// <param name="SQLString"></param> 75 /// <returns></returns> 76 public static bool RunSQL(string SQLString) 77 { 78 using (SQLiteConnection connection = new SQLiteConnection(GetSqlConnectionString())) 79 { 80 try 81 { 82 connection.Open(); 83 SQLiteCommand cmd = connection.CreateCommand(); 84 cmd.CommandText = SQLString; 85 cmd.ExecuteNonQuery(); 86 return true; 87 } 88 catch (Exception ex) 89 { 90 return false; 91 } 92 } 93 } 94 } 95 }View Code
那么到這里,SQLite 的安裝和使用已經結束了,如果你的專案中有部分資料是不需要存盤到線上服務器,只需要留存在用戶本地的話,使用 SQLite 是個很好的選擇,根據專案實際情況而定,
比如,我在用戶使用輸入框時,保存了用戶的搜索記錄,下次用戶再搜索時,可將以往的記錄展示出來,雖然可以保存在快取中,但是快取容易丟失,使用 SQLite 只要資料庫檔案還在,資料沒刪,隨時可以使用,而且性能也還不錯,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/531532.html
標籤:其它
