diff --git a/.gitignore b/.gitignore index 57cc870..7e9a44e 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,5 @@ syntax: glob obj/ [Rr]elease/ glob:.hgignore +storage.ide +sqlite3 diff --git a/Community.CsharpSqlite.SQLiteClient/Community.CsharpSqlite.SQLiteClient.csproj b/Community.CsharpSqlite.SQLiteClient/Community.CsharpSqlite.SQLiteClient.csproj index 1881114..f70e43c 100644 --- a/Community.CsharpSqlite.SQLiteClient/Community.CsharpSqlite.SQLiteClient.csproj +++ b/Community.CsharpSqlite.SQLiteClient/Community.CsharpSqlite.SQLiteClient.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -44,9 +44,10 @@ prompt 4 0168 ; 0169; 0414; 0618; 0649 - x86 + AnyCPU true AllRules.ruleset + false pdbonly @@ -58,6 +59,7 @@ x86 0168 ; 0169; 0414; 0618; 0649 AllRules.ruleset + false bin\NuGet\ @@ -70,6 +72,7 @@ AllRules.ruleset true ..\Eric.pfx + false @@ -82,6 +85,7 @@ + diff --git a/Community.CsharpSqlite.SQLiteClient/app.config b/Community.CsharpSqlite.SQLiteClient/app.config index 73859b0..a89b9c6 100644 --- a/Community.CsharpSqlite.SQLiteClient/app.config +++ b/Community.CsharpSqlite.SQLiteClient/app.config @@ -1,3 +1,3 @@ - + diff --git a/Community.CsharpSqlite.SQLiteClient/src/SqliteDatabase.cs b/Community.CsharpSqlite.SQLiteClient/src/SqliteDatabase.cs new file mode 100644 index 0000000..b97ceca --- /dev/null +++ b/Community.CsharpSqlite.SQLiteClient/src/SqliteDatabase.cs @@ -0,0 +1,177 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; + +namespace Community.CsharpSqlite.SQLiteClient +{ + public class SqliteDatabase + { + public string FileName { get; } + + public bool IsOpen => Connection?.State == ConnectionState.Open; + + + private string ConnectionString { get; set; } + private DbConnection Connection { get; set; } + + + public SqliteDatabase() + { } + + + public SqliteDatabase(string fileName) + { + FileName = fileName; + ConnectionString = $"Data Source=file://{fileName}"; + } + + + + /// + /// Opens the database. + /// + public void Open() + { + Connection = SqliteClientFactory.Instance.CreateConnection(); + Connection.ConnectionString = ConnectionString; + Connection.Open(); + } + + + /// + /// Closes the database. + /// + public void Close() + { + Connection.Close(); + Connection = null; + } + + + /// + /// Execute an arbitrary sql command. + /// + public int ExecuteCommand(string sql) + { + var cmd = Connection.CreateCommand(); + cmd.CommandText = sql; + return cmd.ExecuteNonQuery(); + } + + + + /// + /// Execute sql query and return a DbDataReader with records to iterate over. + /// + public DbDataReader ExecuteReader(string sql) + { + var cmd = Connection.CreateCommand(); + cmd.CommandText = sql; + return cmd.ExecuteReader(); + } + + + + /// + /// Executes the query and returns the first column of the first row in the result + // set returned by the query. All other columns and rows are ignored. + /// + public object ExecuteScalar(string sql) + { + var cmd = Connection.CreateCommand(); + cmd.CommandText = sql; + return cmd.ExecuteScalar(); + } + + + + + public IEnumerable> GetRecords(string sql) + { + var result = new List>(); + using (var reader = ExecuteReader(sql)) + { + while (reader.Read()) + { + var fieldCount = reader.FieldCount; + var rec = new Dictionary(); + for (int fieldNo = 0; fieldNo < fieldCount; fieldNo++) + { + var fieldName = reader.GetName(fieldNo); + var fieldValue = reader.GetValue(fieldNo); + rec[fieldName] = fieldValue; + } + result.Add(rec); + } + } + return result; + + } + + + public IEnumerable ListTables() + { + var result = new List(); + string sql = "SELECT name FROM SQLITE_MASTER where type='table'"; + using (var reader = ExecuteReader(sql)) + { + while (reader.Read()) + { + var tableName = reader["Name"]; + result.Add(tableName.ToString()); + } + } + return result; + } + + + /// + /// Return the results of a sql query as a table + /// + public DataTable GetTable(string sql) + { + DataTable dt = new DataTable(); + using (var reader = ExecuteReader(sql)) + dt.Load(reader); + return dt; + } + + + + + + public void Insert(string tableName, Dictionary rec) + { + if (rec.Count == 0) + throw new Exception("Record contains no data to insert."); + var columns = ""; + var values = ""; + foreach (var val in rec) + { + columns += $" {val.Key},"; + values += $" '{val.Value}',"; + } + columns = columns.Substring(0, columns.Length - 1); + values = values.Substring(0, values.Length - 1); + ExecuteCommand($"insert into {tableName} ({columns}) values ({values});"); + } + + + + public void Update(string tableName, Dictionary rec, string whereClause) + { + if (rec.Count == 0) + throw new Exception("Record contains no data to update."); + if (string.IsNullOrEmpty(whereClause)) + throw new Exception("You must provide a 'where'-clause."); + var values = ""; + foreach (var val in rec) + values += $" {val.Key} = '{val.Value}',"; + values = values.Substring(0, values.Length - 1); + ExecuteCommand($"update {tableName} set {values} where {whereClause};"); + } + + + } +} diff --git a/Community.CsharpSqlite/Community.CsharpSqlite.csproj b/Community.CsharpSqlite/Community.CsharpSqlite.csproj index 91715e7..8afb402 100644 --- a/Community.CsharpSqlite/Community.CsharpSqlite.csproj +++ b/Community.CsharpSqlite/Community.CsharpSqlite.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -46,6 +46,7 @@ 0168 ; 0169; 0414; 0618; 0649 AnyCPU AllRules.ruleset + false pdbonly @@ -57,6 +58,7 @@ AnyCPU 0168 ; 0169; 0414; 0618; 0649 AllRules.ruleset + false bin\NuGet\ @@ -69,6 +71,7 @@ AllRules.ruleset true ..\Eric.pfx + false diff --git a/Community.CsharpSqlite/Community.CsharpSqlite.sln b/Community.CsharpSqlite/Community.CsharpSqlite.sln index 0e7cb6b..a69d1c0 100644 --- a/Community.CsharpSqlite/Community.CsharpSqlite.sln +++ b/Community.CsharpSqlite/Community.CsharpSqlite.sln @@ -1,6 +1,8 @@  -Microsoft Visual Studio Solution File, Format Version 10.00 -# Visual Studio 2008 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2036 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Community.CsharpSqlite", "Community.CsharpSqlite.csproj", "{F1653F20-D47D-4F29-8C55-3C835542AF5F}" EndProject Global @@ -17,4 +19,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F477D35F-87E8-457C-9DF2-78D49958390E} + EndGlobalSection EndGlobal diff --git a/Community.CsharpSqlite/app.config b/Community.CsharpSqlite/app.config index 73859b0..a89b9c6 100644 --- a/Community.CsharpSqlite/app.config +++ b/Community.CsharpSqlite/app.config @@ -1,3 +1,3 @@ - + diff --git a/README.txt b/README.txt deleted file mode 100644 index 9e5bb82..0000000 --- a/README.txt +++ /dev/null @@ -1,9 +0,0 @@ -================================================================================================= - -$Header$ -================================================================================================= - -Project Descriptions -- See the Wiki Projects for details - -Please read HowToCompile for instruction on settings and compiler options - diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..33da281 --- /dev/null +++ b/readme.md @@ -0,0 +1,23 @@ +# Community.CSharp.Sqlite + +This is a Community.Csharp.Sqlite (csharp-sqlite) fork with some additions, most notably `SqliteDatabase`. + +## SqliteDatabase + +An addition to the library to make certain very common operations easier/more convenient. + +## Compiling + +This project compiles in Visual Studio 2017 (Version 15.6.7) under "Any CPU" configuration. + +## Provenance + +The original project by Noah B. Hart can still be found at https://code.google.com/archive/p/csharp-sqlite, but apparently not for much longer. Since the status of the original project is unknown at this time, it is prudent to take precautions to preserve it for posterity. + +This fork was created from https://github.com/ericschultz/csharp-sqlite clone by Eric Schultz. + +- Why this clone and not the original source on Google Code? Because it is easier to fork within GitHub and Eric's version works just fine. + +- Why fork? Why not leave Eric's version alone and just contribute pull requests? Because Eric made his last commit in 2012 and it's unclear if he is "maintaining" the project. We need something that works *today*. + +-AJW, optimax.com \ No newline at end of file