一、SQLite介紹
1、SQLite 簡介
SQLite是一個開源、免費的小型RDBMS(關系型數據庫),能獨立運行、無服務器、零配置、支持事物,用C實現,內存占用較小,支持絕大數的SQL92標准。
這意味着與其他數據庫一樣,您不需要在系統中配置。SQLite 引擎不是一個獨立的進程,可以按應用程序需求進行靜態或動態連接。SQLite 直接訪問其存儲文件。
SQLite 源代碼不受版權限制。SQLite數據庫官方主頁:http://www.sqlite.org/index.html
2、為什么要用 SQLite?
- 不需要一個單獨的服務器進程或操作的系統(無服務器的)。
- SQLite 不需要配置,這意味着不需要安裝或管理。
- 一個完整的 SQLite 數據庫是存儲在一個單一的跨平台的磁盤文件。
- SQLite 是非常小的,是輕量級的,完全配置時小於 400KiB,省略可選功能配置時小於250KiB。
- SQLite 是自給自足的,這意味着不需要任何外部的依賴。
- SQLite 事務是完全兼容 ACID 的,允許從多個進程或線程安全訪問。
- SQLite 支持 SQL92(SQL2)標准的大多數查詢語言的功能。
- SQLite 使用 ANSI-C 編寫的,並提供了簡單和易於使用的 API。
- SQLite 可在 UNIX(Linux, Mac OS-X, Android, iOS)和 Windows(Win32, WinCE, WinRT)中運行。
3、SQLite 局限性
在 SQLite 中,SQL92 不支持的特性如下所示:
4、SQLite GUI客戶端列表:
- SQLite Expert :SQLite administration | SQLite Expert
- SQLite Administrator:http://download.orbmu2k.de/files/sqliteadmin.zip
- SQLiteStudio:
- SQLite Database Browser:
- Navicat Premium :Navicat | 下載 Navicat for SQLite 14 天免費 Windows、macOS 和 Linux 的試用版
二、C#操作數據庫
1、關於SQLite的connection string:
http://www.connectionstrings.com/sqlite/
基本方式:Data Source=c:\mydb.db;Version=3;
2、C#下SQLite操作
驅動dll下載:System.Data.SQLite
也Nuget 直接搜索安裝:System.Data.SQLite.Core
C#使用SQLite步驟:
(1)新建一個project
(2)添加SQLite操作驅動dll引用:System.Data.SQLite.dll
(3)使用API操作SQLite DataBase
using System; using System.Data.SQLite; namespace SQLiteSamples { class Program { //數據庫連接 SQLiteConnection m_dbConnection; static void Main(string[] args) { Program p = new Program(); } public Program() { createNewDatabase(); connectToDatabase(); createTable(); fillTable(); printHighscores(); } //創建一個空的數據庫 void createNewDatabase() { SQLiteConnection.CreateFile("MyDatabase.db");//可以不要此句 } //創建一個連接到指定數據庫 void connectToDatabase() { m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.db;Version=3;");//沒有數據庫則自動創建 m_dbConnection.Open(); } //在指定數據庫中創建一個table void createTable() { string sql = "create table if not exists highscores (name varchar(20), score int)"; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); } //插入一些數據 void fillTable() { string sql = "insert into highscores (name, score) values ('Me', 3000)"; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); sql = "insert into highscores (name, score) values ('Myself', 6000)"; command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); sql = "insert into highscores (name, score) values ('And I', 9001)"; command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); } //使用sql查詢語句,並顯示結果 void printHighscores() { string sql = "select * from highscores order by score desc"; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]); Console.ReadLine(); } } }
三、下載地址:
鏈接:https://pan.baidu.com/s/1iryCFW05hu-U_ZDZkMFA6A
提取碼:q5xy