transaction qry

main
syneffort 2 years ago
parent 2ef8eb4aed
commit b5d8d6fdd6
  1. 55
      SQLite_Console/PsqLiteWrapper/PSqLite.cs
  2. 104
      SQLite_Console/SQLite_Console/Program.cs

@ -73,12 +73,67 @@ namespace PSqLiteWrapper
}
}
public static void ExecuteQuery(string[] queries)
{
if (string.IsNullOrEmpty(DBPath))
throw new ArgumentException("Empty DBPath");
try
{
using (SQLiteConnection conn = Connection)
{
conn.Open();
PSqLite.ExecuteQuery(conn, "BEGIN TRANSACTION;");
for (int i = 0; i < queries.Length; i++)
{
SQLiteCommand command = new SQLiteCommand(queries[i], conn);
command.ExecuteNonQuery();
}
PSqLite.ExecuteQuery(conn, "END TRANSACTION;");
Connection.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
public static void ExecuteQueryAsync(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.ExecuteNonQueryAsync();
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 void ExecuteQueryAsync(SQLiteConnection openedConn, string query)
{
SQLiteCommand command = new SQLiteCommand(query, openedConn);
command.ExecuteNonQueryAsync();
}
public static DataSet ExecuteSelectQuery(string query)
{
if (string.IsNullOrEmpty(DBPath))

@ -47,52 +47,77 @@ namespace SQLite_Console
Console.WriteLine();
watch.Restart();
List<Thread> threads = new List<Thread>();
for (int i = 0; i < 20; i++)
// Cach size
PSqLite.ExecuteQuery("PRAGMA cache_size=10000;");
watch.Stop();
Console.WriteLine($"set cach size to 10000: {watch.ElapsedMilliseconds}ms");
Console.WriteLine();
watch.Restart();
//List<Thread> threads = new List<Thread>();
//for (int i = 0; i < 20; i++)
//{
// threads.Add(new Thread(() => InsertRandomRows($"t{i + 1}", 1000)));
//}
//foreach (Thread thread in threads)
//{
// thread.Start();
//}
Random rnd = new Random();
for (int i = 0; i < 2000; i++)
{
threads.Add(new Thread(() => InsertRandomRows($"t{i + 1}", 1000)));
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);
}
foreach (Thread thread in threads)
watch.Stop();
Console.WriteLine($"insert 2000 row case1: {watch.ElapsedMilliseconds}ms");
Console.WriteLine();
watch.Restart();
rnd = new Random();
using (SQLiteConnection conn = PSqLite.Connection)
{
thread.Start();
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);
}
conn.Close();
}
//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);
//}
watch.Stop();
Console.WriteLine($"insert 2000 row case2: {watch.ElapsedMilliseconds}ms");
Console.WriteLine();
watch.Restart();
//watch.Stop();
//Console.WriteLine($"insert 2000 row case1: {watch.ElapsedMilliseconds}ms");
//Console.WriteLine();
//watch.Restart();
rnd = new Random();
string[] queries = new string[2000];
for (int i = 0; i < 2000; i++)
{
string guid = Guid.NewGuid().ToString();
int age = rnd.Next(1, 100);
string name = $"Robo#{i + 1}";
queries[i] = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')";
}
//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);
// }
// conn.Close();
//}
PSqLite.ExecuteQuery(queries);
//watch.Stop();
//Console.WriteLine($"insert 2000 row case2: {watch.ElapsedMilliseconds}ms");
//Console.WriteLine();
//watch.Restart();
watch.Stop();
Console.WriteLine($"insert 2000 row case3: {watch.ElapsedMilliseconds}ms");
Console.WriteLine();
watch.Restart();
//DataSet select = PSqLite.ExecuteSelectQuery("SELECT * FROM member");
//DataRowCollection rows = select.Tables[0].Rows;
@ -146,15 +171,18 @@ namespace SQLite_Console
using (SQLiteConnection conn = PSqLite.Connection)
{
conn.Open();
PSqLite.ExecuteQuery("BEGIN TRANSACTION;");
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}')";
string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}');";
PSqLite.ExecuteQuery(conn, insertQry);
}
PSqLite.ExecuteQuery("BEGIN TRANSACTION;");
conn.Close();
}

Loading…
Cancel
Save