SQLite技術筆記_使用VB.NET_跨平台輕量級DB_讀取操作可多工、可動態連結_Serverless_實際操作

SQLite不是一個用戶端/伺服器結構的資料庫引擎,而是被整合在用戶程式中。
也可視為一種Library 當中有可以調用一些常見資料庫操作的方法。
不僅遵守  ACID 性質

  1. 單元性 (Atomicity)
  2. 一致性 (Consistency)
  3. 隔離性 (Isolation) 
  4. 持續性 (Durability)

作為嵌入式資料庫,是應用程式、網頁瀏覽器於本地/用戶端儲存資料的常見選擇。


SQLite之設計:

主要優勢:

不像常見的用戶端/伺服器結構資料庫管理系統(多需要跨process做溝通處裡)
SQLite引擎並非緊緊為一個應用程式與之通訊的獨立行程。
又被稱為具有Serverless性質,所以更不需要去擔心Server維護等瑣事。

無論是位在開發上還是部署上都是以 一個function(函式)作為基本單位。

Most SQL database engines are implemented as a separate server process. Programs that want to access the database communicate with the server using some kind of interprocess communication (typically TCP/IP) to send requests to the server and to receive back results. SQLite does not work this way. With SQLite, the process that wants to access the database reads and writes directly from the database files on disk. There is no intermediary server process.

There are advantages and disadvantages to being serverless. The main advantage is that there is no separate server process to install, setup, configure, initialize, manage, and troubleshoot. This is one reason why SQLite is a "zero-configuration" database engine. Programs that use SQLite require no administrative support for setting up the database engine before they are run. Any program that is able to access the disk is able to use an SQLite database.

On the other hand, a database engine that uses a server can provide better protection from bugs in the client application - stray pointers in a client cannot corrupt memory on the server. And because a server is a single persistent process, it is able to control database access with more precision, allowing for finer-grained locking and better concurrency.



SQLite庫連結到程式中,並成為它的一個組成部分。這個庫也可被動態連結。
應用程式可透過程式語言內的API呼叫來使用SQlite的功能,此設計作法在減少資料庫存取延遲上有明顯作用
==>因為在一個單一行程中的函式呼叫 比跨行程通訊更有效率。


SQLite將整個資料庫,包括定義、表、索引以及資料本身,作為一個單獨的、可跨平台使用的檔案儲存在主機中。主要採用了在寫入資料時將整個資料庫檔案加鎖的簡單設計。
儘管寫操作只能串行進行,但SQLite的讀操作可以多工同時進行。


缺失:

多個行程或執行緒是可同時存取同一個資料而沒有問題。
可以同時平行讀取同一個資料庫。
但同一時間只能有一個行程或執行緒進行資料寫入;否則會寫入失敗並得到一個錯誤訊息(或者會自動重試一段時間;自動重試的邏輯以及重試時間的長短是可以設定的)。

SQLite僅部分支援觸發器。儘管它支援大多數的複雜查詢,
但它的ALTER TABLE功能有所限制,不能修改或刪除列。
只能通過重新建立表的方式迂迴進行。

且並不會進行型別檢查,可以把字串Insert到整數列中


實作:

Step1.開啟 Nuget Package  --> 搜尋 SQLite 字眼


按下 Install


下載ING




Step2. Import 表頭


Step3.介面拉一個ListBox 命名為 "listboxEmployee"



Step4.下載一個 Northwind.Sqlite  ---> 副檔名自改
放置於 bin目錄下的 debug / release folder下

http://sqlite.1065341.n5.nabble.com/Northwind-example-database-td59822.html
可以去  網站查 或到 github 看有無人分享



Step5.程式撰寫
使用 ||  去做 String concatenation
https://www.techonthenet.com/sqlite/functions/concatenate.php

用 SqliteConnection( connectionString As String ) 
告知資料來源並開啟連接
The string used to open the connection 
https://docs.microsoft.com/en-us/dotnet/api/microsoft.data.sqlite.sqliteconnection.-ctor?view=msdata-sqlite-2.0.0#Microsoft_Data_Sqlite_SqliteConnection__ctor_System_String_



用 SqliteCommand(String, SqliteConnection)
Public Sub New (commandText As String, connection As SqliteConnection)

https://docs.microsoft.com/en-us/dotnet/api/microsoft.data.sqlite.sqlitecommand.-ctor?view=msdata-sqlite-2.0.0#Microsoft_Data_Sqlite_SqliteCommand__ctor_System_String_Microsoft_Data_Sqlite_SqliteConnection_





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Imports System.Data.SQLite
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadEmployeeData()
    End Sub

    Public Sub LoadEmployeeData()
        Dim connectStr As String = "Data Source=Northwind.sqlite"
        Dim mSQL As String = "Select EmployeeID, FirstName || ' ' || LastName as FullName From Employees"
        Dim dt As DataTable = Nothing
        Dim ds As New DataSet

        Try
            Using connect As New SQLiteConnection(connectStr)
                Using cmd As New SQLiteCommand(mSQL, connect)
                    connect.Open()
                    Using da As New SQLiteDataAdapter(cmd)
                        da.Fill(ds)
                        dt = ds.Tables(0)
                    End Using
                End Using
            End Using

            listboxEmployee.ValueMember = "EmployeeID"
            listboxEmployee.DisplayMember = "FullName"
            listboxEmployee.DataSource = dt
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub

    
End Class

原先於SQL Server當中開啟原始資料

執行結果




參考Link:
SQLite wiki
https://zh.wikipedia.org/wiki/SQLite

SQLite Official website
https://www.sqlite.org/serverless.html
https://www.sqlite.org/about.html


ACID 屬性
https://msdn.microsoft.com/zh-tw/library/aa719484(v=vs.71).aspx

SQL-99標準
http://lolikitty.pixnet.net/blog/post/39547028-sql-99標準
SQLite 簡介
http://blog.xuite.net/yan.kee/CSharp/27642207-SQLite+簡介

留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題