From 702bac7e6efddc546b73b72bb44bb191961f4791 Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 8 Dec 2022 13:41:37 +0900 Subject: [PATCH] library --- SQLite_Console/PsqLiteWrapper/App.config | 19 ++ SQLite_Console/PsqLiteWrapper/PSqLite.cs | 116 ++++++++++++ .../PsqLiteWrapper/PSqLiteWrapper.csproj | 81 +++++++++ .../PsqLiteWrapper/Properties/AssemblyInfo.cs | 36 ++++ SQLite_Console/PsqLiteWrapper/packages.config | 9 + SQLite_Console/SQLite_Console.sln | 10 +- SQLite_Console/SQLite_Console/Program.cs | 167 +++++++++++++----- .../SQLite_Console/SQLite_Console.csproj | 6 + 8 files changed, 398 insertions(+), 46 deletions(-) create mode 100644 SQLite_Console/PsqLiteWrapper/App.config create mode 100644 SQLite_Console/PsqLiteWrapper/PSqLite.cs create mode 100644 SQLite_Console/PsqLiteWrapper/PSqLiteWrapper.csproj create mode 100644 SQLite_Console/PsqLiteWrapper/Properties/AssemblyInfo.cs create mode 100644 SQLite_Console/PsqLiteWrapper/packages.config diff --git a/SQLite_Console/PsqLiteWrapper/App.config b/SQLite_Console/PsqLiteWrapper/App.config new file mode 100644 index 0000000..497a120 --- /dev/null +++ b/SQLite_Console/PsqLiteWrapper/App.config @@ -0,0 +1,19 @@ + + + + +
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/SQLite_Console/PsqLiteWrapper/PSqLite.cs b/SQLite_Console/PsqLiteWrapper/PSqLite.cs new file mode 100644 index 0000000..53f1e23 --- /dev/null +++ b/SQLite_Console/PsqLiteWrapper/PSqLite.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SQLite; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PSqLiteWrapper +{ + public static class PSqLite + { + public static string DBPath { get; set; } + + private static string ConnString + { + get { return $@"Data Source = {DBPath};"; } + } + + public static SQLiteConnection Connection + { + get + { + if (string.IsNullOrEmpty(DBPath)) + throw new ArgumentException("Empty DBPath"); + + try + { + return new SQLiteConnection(ConnString); + } + catch (Exception ex) + { + throw ex; + } + } + } + + public static void CreateDB() + { + if (string.IsNullOrEmpty(DBPath)) + throw new ArgumentException("Empty DBPath"); + + try + { + if (!File.Exists(DBPath)) + SQLiteConnection.CreateFile(DBPath); + } + catch (Exception ex) + { + throw ex; + } + } + + public static void ExecuteQuery(string query) + { + if (string.IsNullOrEmpty(DBPath)) + throw new ArgumentException("Empty DBPath"); + + try + { + using (SQLiteConnection conn = Connection) + { + conn.Open(); + SQLiteCommand command = new SQLiteCommand(query, conn); + command.ExecuteNonQuery(); + Connection.Close(); + } + } + catch (Exception ex) + { + throw ex; + } + } + + public static void ExecuteQuery(SQLiteConnection openedConn, string query) + { + SQLiteCommand command = new SQLiteCommand(query, openedConn); + command.ExecuteNonQuery(); + } + + public static DataSet ExecuteSelectQuery(string query) + { + if (string.IsNullOrEmpty(DBPath)) + throw new ArgumentException("Empty DBPath"); + + try + { + using (SQLiteConnection conn = Connection) + { + DataSet dataSet = new DataSet(); + + conn.Open(); + SQLiteDataAdapter adapter = new SQLiteDataAdapter(query, conn); + adapter.Fill(dataSet); + Connection.Close(); + + return dataSet; + } + } + catch (Exception ex) + { + throw ex; + } + } + + public static DataSet ExecuteSelectQuery(SQLiteConnection openedConn, string query) + { + DataSet dataSet = new DataSet(); + SQLiteDataAdapter adapter = new SQLiteDataAdapter(query, openedConn); + adapter.Fill(dataSet); + + return dataSet; + } + } +} diff --git a/SQLite_Console/PsqLiteWrapper/PSqLiteWrapper.csproj b/SQLite_Console/PsqLiteWrapper/PSqLiteWrapper.csproj new file mode 100644 index 0000000..f15cdf9 --- /dev/null +++ b/SQLite_Console/PsqLiteWrapper/PSqLiteWrapper.csproj @@ -0,0 +1,81 @@ + + + + + + Debug + AnyCPU + {24FE639E-FC41-4AE9-8682-838E07E5BCA9} + Library + Properties + PSqLiteWrapper + PSqLiteWrapper + v4.5 + 512 + true + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.dll + + + ..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll + + + + + + ..\packages\Stub.System.Data.SQLite.Core.NetFramework.1.0.117.0\lib\net45\System.Data.SQLite.dll + + + ..\packages\System.Data.SQLite.EF6.1.0.117.0\lib\net45\System.Data.SQLite.EF6.dll + + + ..\packages\System.Data.SQLite.Linq.1.0.117.0\lib\net45\System.Data.SQLite.Linq.dll + + + + + + + + + + + + + + + + + + + + 이 프로젝트는 이 컴퓨터에 없는 NuGet 패키지를 참조합니다. 해당 패키지를 다운로드하려면 NuGet 패키지 복원을 사용하십시오. 자세한 내용은 http://go.microsoft.com/fwlink/?LinkID=322105를 참조하십시오. 누락된 파일은 {0}입니다. + + + + + + + + \ No newline at end of file diff --git a/SQLite_Console/PsqLiteWrapper/Properties/AssemblyInfo.cs b/SQLite_Console/PsqLiteWrapper/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..44b15f4 --- /dev/null +++ b/SQLite_Console/PsqLiteWrapper/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("PSqLiteWrapper")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("PsqLiteWrapper")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("24fe639e-fc41-4ae9-8682-838e07e5bca9")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/SQLite_Console/PsqLiteWrapper/packages.config b/SQLite_Console/PsqLiteWrapper/packages.config new file mode 100644 index 0000000..ad6d85a --- /dev/null +++ b/SQLite_Console/PsqLiteWrapper/packages.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/SQLite_Console/SQLite_Console.sln b/SQLite_Console/SQLite_Console.sln index b282dae..a4b1f5a 100644 --- a/SQLite_Console/SQLite_Console.sln +++ b/SQLite_Console/SQLite_Console.sln @@ -1,10 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.32630.194 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLite_Console", "SQLite_Console\SQLite_Console.csproj", "{A4457E3F-0254-45B1-B1AF-6196AA25B9B9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSqLiteWrapper", "PsqLiteWrapper\PSqLiteWrapper.csproj", "{24FE639E-FC41-4AE9-8682-838E07E5BCA9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {A4457E3F-0254-45B1-B1AF-6196AA25B9B9}.Debug|Any CPU.Build.0 = Debug|Any CPU {A4457E3F-0254-45B1-B1AF-6196AA25B9B9}.Release|Any CPU.ActiveCfg = Release|Any CPU {A4457E3F-0254-45B1-B1AF-6196AA25B9B9}.Release|Any CPU.Build.0 = Release|Any CPU + {24FE639E-FC41-4AE9-8682-838E07E5BCA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {24FE639E-FC41-4AE9-8682-838E07E5BCA9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {24FE639E-FC41-4AE9-8682-838E07E5BCA9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {24FE639E-FC41-4AE9-8682-838E07E5BCA9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SQLite_Console/SQLite_Console/Program.cs b/SQLite_Console/SQLite_Console/Program.cs index c73d86d..e45e7c2 100644 --- a/SQLite_Console/SQLite_Console/Program.cs +++ b/SQLite_Console/SQLite_Console/Program.cs @@ -1,10 +1,15 @@ -using System; +using PSqLiteWrapper; +using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using System.Diagnostics; +using System.IO; using System.Linq; +using System.Reflection; using System.Text; +using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; namespace SQLite_Console @@ -13,60 +18,106 @@ namespace SQLite_Console { static void Main(string[] args) { - string connString = @"Data Source = D:\SQLite\mydb.db"; - using (SQLiteConnection conn = new SQLiteConnection(connString)) + string dbFileName = "mydb.db"; + + string path = Assembly.GetExecutingAssembly().CodeBase; + string dir = Path.GetDirectoryName(path).Replace(@"file:\", ""); + + Stopwatch watch = new Stopwatch(); + watch.Start(); + + PSqLite.DBPath = $@"{dir}\{dbFileName}"; + PSqLite.CreateDB(); + + watch.Stop(); + Console.WriteLine($"database check: {watch.ElapsedMilliseconds}ms"); + Console.WriteLine(); + watch.Restart(); + + // Create table + PSqLite.ExecuteQuery( + @"CREATE TABLE 'member' ( + 'name' TEXT, + 'age' INTEGER, + 'guid' TEXT + );"); + + watch.Stop(); + Console.WriteLine($"create table: {watch.ElapsedMilliseconds}ms"); + Console.WriteLine(); + watch.Restart(); + + List threads = new List(); + for (int i = 0; i < 20; i++) { - Stopwatch watch = new Stopwatch(); - watch.Start(); + threads.Add(new Thread(() => InsertRandomRows($"t{i + 1}", 1000))); + } - conn.Open(); + foreach (Thread thread in threads) + { + thread.Start(); + } - watch.Stop(); - Console.WriteLine($"open: {watch.ElapsedMilliseconds}ms"); - Console.WriteLine(); - watch.Restart(); + //Random rnd = new Random(); + //for (int i = 0; i < 2000; i++) + //{ + // string guid = Guid.NewGuid().ToString(); + // int age = rnd.Next(1, 100); + // string name = $"Robo#{i + 1}"; + // string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; + // PSqLite.ExecuteQuery(insertQry); + //} - Random rnd = new Random(); - for (int i = 0; i < 2000; i++) - { - string guid = Guid.NewGuid().ToString(); - int age = rnd.Next(1, 100); - string name = $"Robot#{i + 1}"; - string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; - SQLiteCommand cmd = new SQLiteCommand(insertQry, conn); - cmd.ExecuteNonQuery(); - } + //watch.Stop(); + //Console.WriteLine($"insert 2000 row case1: {watch.ElapsedMilliseconds}ms"); + //Console.WriteLine(); + //watch.Restart(); - watch.Stop(); - Console.WriteLine($"insert: {watch.ElapsedMilliseconds}ms"); - Console.WriteLine(); - watch.Restart(); + //rnd = new Random(); + //using (SQLiteConnection conn = PSqLite.Connection) + //{ + // conn.Open(); + // for (int i = 0; i < 2000; i++) + // { + // string guid = Guid.NewGuid().ToString(); + // int age = rnd.Next(1, 100); + // string name = $"Robo#{i + 1}"; + // string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; + // PSqLite.ExecuteQuery(conn, insertQry); + // } - DataSet ds = new DataSet(); - string selectQry = "SELECT * FROM member"; - SQLiteDataAdapter adt = new SQLiteDataAdapter(selectQry, conn); - adt.Fill(ds); + // conn.Close(); + //} - DataRowCollection rows = ds.Tables[0].Rows; - PrintRows(rows); + //watch.Stop(); + //Console.WriteLine($"insert 2000 row case2: {watch.ElapsedMilliseconds}ms"); + //Console.WriteLine(); + //watch.Restart(); - watch.Stop(); - Console.WriteLine($"select all: {watch.ElapsedMilliseconds}ms"); - Console.WriteLine(); - watch.Restart(); + //DataSet select = PSqLite.ExecuteSelectQuery("SELECT * FROM member"); + //DataRowCollection rows = select.Tables[0].Rows; + ////PrintRows(rows); - selectQry = "SELECT * FROM member WHERE age < 50"; - adt = new SQLiteDataAdapter(selectQry, conn); - adt.Fill(ds); + //watch.Stop(); + //Console.WriteLine($"select all: {watch.ElapsedMilliseconds}ms"); + //Console.WriteLine(); + //watch.Restart(); - rows = ds.Tables[0].Rows; - PrintRows(rows); + //PSqLite.ExecuteQuery("UPDATE member SET name = 'minor' WHERE age <= 20"); - watch.Stop(); - Console.WriteLine($"select under 50: {watch.ElapsedMilliseconds}ms"); - Console.WriteLine(); - watch.Restart(); - } + //watch.Stop(); + //Console.WriteLine($"update under 20: {watch.ElapsedMilliseconds}ms"); + //Console.WriteLine(); + + //select = PSqLite.ExecuteSelectQuery("SELECT * FROM member WHERE age <= 20"); + //rows = select.Tables[0].Rows; + + //watch.Stop(); + //Console.WriteLine($"select under 20: {watch.ElapsedMilliseconds}ms"); + //Console.WriteLine(); + //watch.Restart(); + + //PrintRows(rows); Console.ReadKey(); } @@ -83,5 +134,33 @@ namespace SQLite_Console Console.WriteLine($"name: {name}, age: {age}, guid: {guid}"); } } + + private static void InsertRandomRows(string identifier, int count) + { + Console.WriteLine(identifier); + + Stopwatch watch = new Stopwatch(); + watch.Start(); + + Random rnd = new Random(); + using (SQLiteConnection conn = PSqLite.Connection) + { + conn.Open(); + for (int i = 0; i < count; i++) + { + string guid = Guid.NewGuid().ToString(); + int age = rnd.Next(1, 100); + string name = $"{identifier} Robo#{i + 1}"; + string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; + PSqLite.ExecuteQuery(conn, insertQry); + } + + conn.Close(); + } + + watch.Stop(); + Console.WriteLine($"insert 2000 row for {identifier}: {watch.ElapsedMilliseconds}ms"); + Console.WriteLine(); + } } } diff --git a/SQLite_Console/SQLite_Console/SQLite_Console.csproj b/SQLite_Console/SQLite_Console/SQLite_Console.csproj index 86ce942..f031ec0 100644 --- a/SQLite_Console/SQLite_Console/SQLite_Console.csproj +++ b/SQLite_Console/SQLite_Console/SQLite_Console.csproj @@ -68,6 +68,12 @@ + + + {24fe639e-fc41-4ae9-8682-838e07e5bca9} + PSqLiteWrapper + +