SQLite,是一款輕型的關係型數據庫。它的設計目標是嵌入式。html
它可以支持Windows/Linux/Unix等等主流的操做系統,同時可以跟不少程序語言相結合,好比 C++、C#、Object-C、PHP、Java等。sql
我是作手遊開發的,在手機上使用SQLite 存儲數據是很方便的。 數據庫
Tips:函數
1.根據本身的電腦是64bit或者32bit 以及使用的.NET Freamwork版本下載相應的的System.Data.SQLite。 下載解壓文件如:工具
安裝在指定目錄後生成多個dll文件:測試
本文測試用的只有 System.Data.SQLite.dll 。對於SQLite庫數據的增刪改查已經足夠用了。spa
2.SQLite數據庫文件以.db爲後綴。安裝SQLite可視化工具後如圖:操作系統
mydb是我測試用的數據庫。設計
對SQLite的增、刪、改、查。code
測試環境:VS2015 語言:C#
建立控制檯新工程,引入System.Data.SQLite.dll 文件。
1.編寫SQLite 操做類, 對SQLite數據操做進行封裝爲 SqLiteHelper,代碼以下:
/// <summary> /// SQLite 操做類 /// </summary> class SqLiteHelper { /// <summary> /// 數據庫鏈接定義 /// </summary> private SQLiteConnection dbConnection; /// <summary> /// SQL命令定義 /// </summary> private SQLiteCommand dbCommand; /// <summary> /// 數據讀取定義 /// </summary> private SQLiteDataReader dataReader; /// <summary> /// 構造函數 /// </summary> /// <param name="connectionString">鏈接SQLite庫字符串</param> public SqLiteHelper(string connectionString) { try { dbConnection = new SQLiteConnection(connectionString); dbConnection.Open(); }catch(Exception e) { Log(e.ToString()); } } /// <summary> /// 執行SQL命令 /// </summary> /// <returns>The query.</returns> /// <param name="queryString">SQL命令字符串</param> public SQLiteDataReader ExecuteQuery(string queryString) { try { dbCommand = dbConnection.CreateCommand(); dbCommand.CommandText = queryString; dataReader = dbCommand.ExecuteReader(); }catch(Exception e) { Log(e.Message); } return dataReader; } /// <summary> /// 關閉數據庫鏈接 /// </summary> public void CloseConnection() { //銷燬Commend if(dbCommand !=null) { dbCommand.Cancel(); } dbCommand = null; //銷燬Reader if(dataReader != null) { dataReader.Close(); } dataReader = null; //銷燬Connection if(dbConnection != null) { dbConnection.Close(); } dbConnection = null; } /// <summary> /// 讀取整張數據表 /// </summary> /// <returns>The full table.</returns> /// <param name="tableName">數據表名稱</param> public SQLiteDataReader ReadFullTable(string tableName) { string queryString = "SELECT * FROM " + tableName; return ExecuteQuery(queryString); } /// <summary> /// 向指定數據表中插入數據 /// </summary> /// <returns>The values.</returns> /// <param name="tableName">數據表名稱</param> /// <param name="values">插入的數值</param> public SQLiteDataReader InsertValues(string tableName, string[] values) { //獲取數據表中字段數目 int fieldCount = ReadFullTable(tableName).FieldCount; //當插入的數據長度不等於字段數目時引起異常 if (values.Length != fieldCount) { throw new SQLiteException("values.Length!=fieldCount"); } string queryString = "INSERT INTO " + tableName + " VALUES (" + "'"+ values[0]+"'"; for (int i = 1; i < values.Length; i++) { queryString += ", " + "'"+values[i] + "'"; } queryString += " )"; return ExecuteQuery(queryString); } /// <summary> /// 更新指定數據表內的數據 /// </summary> /// <returns>The values.</returns> /// <param name="tableName">數據表名稱</param> /// <param name="colNames">字段名</param> /// <param name="colValues">字段名對應的數據</param> /// <param name="key">關鍵字</param> /// <param name="value">關鍵字對應的值</param> /// <param name="operation">運算符:=,<,>,...,默認「=」</param> public SQLiteDataReader UpdateValues(string tableName, string[] colNames, string[] colValues, string key, string value, string operation="=") { //當字段名稱和字段數值不對應時引起異常 if (colNames.Length != colValues.Length) { throw new SQLiteException("colNames.Length!=colValues.Length"); } string queryString = "UPDATE " + tableName + " SET " + colNames[0] + "=" + "'" + colValues[0] + "'"; for (int i = 1; i < colValues.Length; i++) { queryString += ", " + colNames[i] + "=" + "'" + colValues[i] + "'"; } queryString += " WHERE " + key + operation + "'" + value + "'"; return ExecuteQuery(queryString); } /// <summary> /// 刪除指定數據表內的數據 /// </summary> /// <returns>The values.</returns> /// <param name="tableName">數據表名稱</param> /// <param name="colNames">字段名</param> /// <param name="colValues">字段名對應的數據</param> public SQLiteDataReader DeleteValuesOR(string tableName, string[] colNames, string[] colValues, string[] operations) { //當字段名稱和字段數值不對應時引起異常 if (colNames.Length != colValues.Length || operations.Length != colNames.Length || operations.Length != colValues.Length) { throw new SQLiteException("colNames.Length!=colValues.Length || operations.Length!=colNames.Length || operations.Length!=colValues.Length"); } string queryString = "DELETE FROM " + tableName + " WHERE " + colNames[0] + operations[0] + "'" + colValues[0] + "'"; for (int i = 1; i < colValues.Length; i++) { queryString += "OR " + colNames[i] + operations[0] + "'" + colValues[i] + "'"; } return ExecuteQuery(queryString); } /// <summary> /// 刪除指定數據表內的數據 /// </summary> /// <returns>The values.</returns> /// <param name="tableName">數據表名稱</param> /// <param name="colNames">字段名</param> /// <param name="colValues">字段名對應的數據</param> public SQLiteDataReader DeleteValuesAND(string tableName, string[] colNames, string[] colValues, string[] operations) { //當字段名稱和字段數值不對應時引起異常 if (colNames.Length != colValues.Length || operations.Length != colNames.Length || operations.Length != colValues.Length) { throw new SQLiteException("colNames.Length!=colValues.Length || operations.Length!=colNames.Length || operations.Length!=colValues.Length"); } string queryString = "DELETE FROM " + tableName + " WHERE " + colNames[0] + operations[0] + "'" + colValues[0] + "'"; for (int i = 1; i < colValues.Length; i++) { queryString += " AND " + colNames[i] + operations[i] + "'" + colValues[i] + "'"; } return ExecuteQuery(queryString); } /// <summary> /// 建立數據表 /// </summary> + /// <returns>The table.</returns> /// <param name="tableName">數據表名</param> /// <param name="colNames">字段名</param> /// <param name="colTypes">字段名類型</param> public SQLiteDataReader CreateTable(string tableName, string[] colNames, string[] colTypes) { string queryString = "CREATE TABLE IF NOT EXISTS " + tableName + "( " + colNames[0] + " " + colTypes[0]; for (int i = 1; i < colNames.Length; i++) { queryString += ", " + colNames[i] + " " + colTypes[i]; } queryString += " ) "; return ExecuteQuery(queryString); } /// <summary> /// Reads the table. /// </summary> /// <returns>The table.</returns> /// <param name="tableName">Table name.</param> /// <param name="items">Items.</param> /// <param name="colNames">Col names.</param> /// <param name="operations">Operations.</param> /// <param name="colValues">Col values.</param> public SQLiteDataReader ReadTable(string tableName, string[] items, string[] colNames, string[] operations, string[] colValues) { string queryString = "SELECT " + items[0]; for (int i = 1; i < items.Length; i++) { queryString += ", " + items[i]; } queryString += " FROM " + tableName + " WHERE " + colNames[0] + " " + operations[0] + " " + colValues[0]; for (int i = 0; i < colNames.Length; i++) { queryString += " AND " + colNames[i] + " " + operations[i] + " " + colValues[0] + " "; } return ExecuteQuery(queryString); } /// <summary> /// 本類log /// </summary> /// <param name="s"></param> static void Log(string s) { Console.WriteLine("class SqLiteHelper:::" + s); } }
Tips:1.別忘了引入:using System.Data.SQLite;
2.引入System.Data.SQLite.dll的同時,須要將 System.Data.SQLite.dll.config 文件放在同級目錄下。不然加載dll文件時報錯。
2.在工程主函數Main裏面添加測試代碼
class Program { private static SqLiteHelper sql; static void Main(string[] args) { sql = new SqLiteHelper("data source=mydb.db"); //建立名爲table1的數據表 sql.CreateTable("table1", new string[] { "ID", "Name", "Age", "Email" }, new string[] { "INTEGER", "TEXT", "INTEGER", "TEXT" }); //插入兩條數據 sql.InsertValues("table1",new string[]{"1","張三","22","Zhang@163.com" }); sql.InsertValues("table1", new string[] { "2", "李四", "25", "Li4@163.com" }); //更新數據,將Name="張三"的記錄中的Name改成"Zhang3" sql.UpdateValues("table1", new string[] { "Name" }, new string[] { "ZhangSan" }, "Name", "Zhang3"); //刪除Name="張三"且Age=26的記錄,DeleteValuesOR方法相似 sql.DeleteValuesAND("table1", new string[] { "Name", "Age" }, new string[] { "張三", "22" }, new string[] { "=", "=" }); //讀取整張表 SQLiteDataReader reader = sql.ReadFullTable("table1"); while (reader.Read()) { //讀取ID Log(""+reader.GetInt32(reader.GetOrdinal("ID"))); //讀取Name Log(""+reader.GetString(reader.GetOrdinal("Name"))); //讀取Age Log(""+reader.GetInt32(reader.GetOrdinal("Age"))); //讀取Email Log(reader.GetString(reader.GetOrdinal("Email"))); } while (true) { Console.ReadLine(); } } static void Log(string s) { Console.WriteLine(""+s); } }
Tips:1.別忘了引入:using System.Data.SQLite;
歡迎討論!