網(wǎng)站首頁 編程語言 正文
1、SQLite介紹
SQLite,是一款輕型的數(shù)據(jù)庫,是遵守的ACID關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它包含在一個(gè)相對(duì)小的C庫中。它的設(shè)計(jì)目標(biāo)嵌入式是的,而且已經(jīng)在很多中使用了它,它占用資源非常的低,在嵌入式設(shè)備中,可能只需要幾百K的內(nèi)存就夠了。它能夠支持Windows/Linux/Unix等等主流的操作系統(tǒng),同時(shí)能夠跟很多程序語言相結(jié)合,比如 Tcl、C#、PHP、Java等。
SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.
SQLite是一個(gè)開源、免費(fèi)的小型RDBMS(關(guān)系型數(shù)據(jù)庫),能獨(dú)立運(yùn)行、無服務(wù)器、零配置、支持事物,用C實(shí)現(xiàn),內(nèi)存占用較小,支持絕大數(shù)的SQL92標(biāo)準(zhǔn)。
SQLite數(shù)據(jù)庫官方主頁:http://www.sqlite.org/index.html
2、C#下調(diào)用SQLite數(shù)據(jù)庫
在NuGet程序包內(nèi),搜索System.Data.Sqlite,安裝Sqlite類庫
3、在C#程序內(nèi)添加SqliteHelper
sqliteHelper中主要用到2個(gè)方法:
? a、ExecuteNonQuery 執(zhí)行Insert,Update、Delete、創(chuàng)建庫等操作,返回值是數(shù)據(jù)庫影響的行數(shù)
? b、ExecuteDataSet執(zhí)行Select操作,返回查詢數(shù)據(jù)集
public class SQLiteHelper { public static string ConnectionString = "Data Source =" + Environment.CurrentDirectory + @"\database.db" + ";Pooling = true; FailIfMissing = true"; /// <summary> /// 執(zhí)行數(shù)據(jù)庫操作(新增、更新或刪除) /// </summary> /// <param name="cmdstr">連接字符串</param> /// <param name="cmdParms">SqlCommand對(duì)象</param> /// <returns>受影響的行數(shù)</returns> public int ExecuteNonQuery(string cmdstr, params SQLiteParameter[] cmdParms) { int result = 0; using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { SQLiteTransaction trans = null; SQLiteCommand cmd = new SQLiteCommand(cmdstr); PrepareCommand(cmd, conn, ref trans, true, cmd.CommandType, cmd.CommandText, cmdParms); try { result = cmd.ExecuteNonQuery(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } /// <summary> /// 預(yù)處理Command對(duì)象,數(shù)據(jù)庫鏈接,事務(wù),需要執(zhí)行的對(duì)象,參數(shù)等的初始化 /// </summary> /// <param name="cmd">Command對(duì)象</param> /// <param name="conn">Connection對(duì)象</param> /// <param name="trans">Transcation對(duì)象</param> /// <param name="useTrans">是否使用事務(wù)</param> /// <param name="cmdType">SQL字符串執(zhí)行類型</param> /// <param name="cmdText">SQL Text</param> /// <param name="cmdParms">SQLiteParameters to use in the command</param> private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, ref SQLiteTransaction trans, bool useTrans, CommandType cmdType, string cmdText, params SQLiteParameter[] cmdParms) { try { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (useTrans) { trans = conn.BeginTransaction(IsolationLevel.ReadCommitted); cmd.Transaction = trans; } cmd.CommandType = cmdType; if (cmdParms != null) { foreach (SQLiteParameter parm in cmdParms) cmd.Parameters.Add(parm); } } catch { } } /// <summary> /// 數(shù)據(jù)庫查詢 /// </summary> /// <param name="cmdstr">sql語句</param> /// <param name="tableName">表名</param> /// <returns>DataSet對(duì)象</returns> public DataSet ExecuteDataSet(string cmdstr) { DataSet ds = new DataSet(); SQLiteConnection conn = new SQLiteConnection(ConnectionString); SQLiteTransaction trans = null; SQLiteCommand cmd = new SQLiteCommand(cmdstr); PrepareCommand(cmd, conn, ref trans, false, cmd.CommandType, cmd.CommandText); try { SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd); sda.Fill(ds); } catch (Exception ex) { throw ex; } finally { if (cmd.Connection != null) { if (cmd.Connection.State == ConnectionState.Open) { cmd.Connection.Close(); } } } return ds; }
4、Sqlite部分技巧?
? a、SQLiteConnection類的CreateFile方法,在程序內(nèi)動(dòng)態(tài)創(chuàng)建數(shù)據(jù)庫文件,通過下面的方法即可創(chuàng)建出Analysis.db名稱的數(shù)據(jù)庫
/// <summary> /// 數(shù)據(jù)庫路徑 /// </summary> private static string databasepath = AppDomain.CurrentDomain.BaseDirectory + "DataBase\\"; /// <summary> /// 數(shù)據(jù)庫名稱 /// </summary> private const string databasename = "Analysis.db"; /// <summary> /// 創(chuàng)建數(shù)據(jù)庫 /// </summary> public static void CreateDataBase() { try { if (!File.Exists(databasepath + databasename)) { if (!Directory.Exists(databasepath)) Directory.CreateDirectory(databasepath); SQLiteConnection.CreateFile(databasepath + databasename); LogHelper.Info("創(chuàng)建數(shù)據(jù)庫:" + databasename + "成功!"); } } catch (Exception ex) { LogHelper.Debug(ex); } }
? b、在寫入高頻數(shù)據(jù)的時(shí)候,需要使用事務(wù),如果反復(fù)進(jìn)行(打開->插入>關(guān)閉)操作,sqlite效率1秒鐘插入也就2條,使用程序進(jìn)行插入就會(huì)發(fā)現(xiàn)輸入的頻率遠(yuǎn)低于獲取到的數(shù)據(jù),大量的數(shù)據(jù)被緩存到內(nèi)存中,為了處理入庫的速度慢,就要用到事務(wù),事務(wù)流程:
? ? ①打開連接
? ? ②開始事務(wù)
? ? ③循環(huán)在內(nèi)存中執(zhí)行插入命令
? ? ④提交事務(wù)寫入本地文件,如果出錯(cuò)回滾事務(wù)
? ? ⑤關(guān)閉連接
代碼見下圖,開始事務(wù)后通過SQLiteCommand的ExecuteNonQuery()方法進(jìn)行內(nèi)存提交
using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { using (SQLiteCommand cmd = new SQLiteCommand()) { DbTransaction trans = null; try { cmd.Connection = conn; conn.Open(); //開啟事務(wù) using (trans = conn.BeginTransaction()) { while (_list.Count > 0) { GpsDataClass _gps = _list[0]; try { if (_gps != null) { SQLiteHelper sh = new SQLiteHelper(cmd); var dic = new Dictionary<string, object>(); dic["CarPlate"] = _gps.CarPlate; dic["CarIpAddress"] = _gps.CarIpAddress; dic["PosX1"] = _gps.PosX1; dic["PosY1"] = _gps.PosY1; dic["PosZ1"] = _gps.PosZ1; dic["Heading1"] = _gps.Heading1; dic["PosStatus1"] = _gps.PosStatus1; dic["NumF1"] = _gps.NumF1; dic["NumB1"] = _gps.NumB1; dic["PosX2"] = _gps.PosX2; dic["PosY2"] = _gps.PosY2; dic["PosZ2"] = _gps.PosZ2; dic["Heading2"] = _gps.Heading2; dic["PosStatus2"] = _gps.PosStatus2; dic["NumF2"] = _gps.NumF2; dic["NumB2"] = _gps.NumB2; dic["Speed"] = _gps.Speed; dic["Signal"] = _gps.Signal; dic["NowTime"] = _gps.NowTime; sh.Insert("GpsRecord", dic); _list.RemoveAt(0); } } catch (Exception ex) { LogHelper.Debug(ex); } } trans.Commit(); } } catch (Exception ex) { trans.Rollback(); LogHelper.Debug(ex); } conn.Close(); } }
原文鏈接:https://blog.csdn.net/evint888/article/details/122331724
相關(guān)推薦
- 2022-07-02 ansible模塊之include_tasks:為什么加了tags后導(dǎo)入的任務(wù)沒有執(zhí)行?
- 2022-09-08 Go語言中的閉包詳解_Golang
- 2022-12-11 python中windows鏈接linux執(zhí)行命令并獲取執(zhí)行狀態(tài)的問題小結(jié)_python
- 2021-12-09 Jenkins+GitLab+Docker持續(xù)集成LNMP
- 2022-11-01 C語言strlen,strcpy,strcmp,strcat,strstr字符串操作函數(shù)實(shí)現(xiàn)_C 語
- 2022-08-22 Python3.9用pip安裝wordcloud庫失敗的解決過程_python
- 2022-12-01 .NET?Core部署為Windows服務(wù)的詳細(xì)步驟_實(shí)用技巧
- 2022-10-13 Python常用圖像形態(tài)學(xué)操作詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支