Now the SQLITE database is operationnal and is fed with driver infos

This commit is contained in:
2023-05-17 15:23:19 +02:00
parent ac65c7b4f3
commit e727dc9301
5 changed files with 230 additions and 71 deletions
+107 -20
View File
@@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.IO;
using System.Windows.Forms;
namespace Test_Merge
{
@@ -13,6 +14,8 @@ namespace Test_Merge
private const string DATABASE_FOLDER = "./Data";
private const string DATABASE_FILE = "/database.sqlite";
private const string CONNECTION_STRING = "Data Source=" + DATABASE_FOLDER + DATABASE_FILE + ";Version=3;";
private SQLiteConnection Connection;
public SqliteStorage()
{
Load();
@@ -29,36 +32,34 @@ namespace Test_Merge
else
{
//We are not using the existing DataBase
File.Delete(DATABASE_FOLDER+DATABASE_FILE);
File.Delete(DATABASE_FOLDER + DATABASE_FILE);
}
using (var connection = new SQLiteConnection(CONNECTION_STRING))
{
connection.Open();
Connection = new SQLiteConnection(CONNECTION_STRING);
Connection.Open();
//Create the drivers table
string createDriversTableQuery = @"CREATE TABLE IF NOT EXISTS Drivers
//Create the drivers table
string createDriversTableQuery = @"CREATE TABLE IF NOT EXISTS Drivers
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
Name VARCHAR NOT NULL);";
using (var command = new SQLiteCommand(createDriversTableQuery, connection))
{
command.ExecuteNonQuery();
}
using (var command = new SQLiteCommand(createDriversTableQuery, Connection))
{
command.ExecuteNonQuery();
}
//Create the drivers table
string createPitstopTableQuery = @"CREATE TABLE Pitstops
//Create the drivers table
string createPitstopTableQuery = @"CREATE TABLE Pitstops
(Lap INTEGER NOT NULL,
DriverID INTEGER NOT NULL,
Tyre VARCHAR,
PRIMARY KEY (Lap,DriverID));";
using (var command = new SQLiteCommand(createPitstopTableQuery, connection))
{
command.ExecuteNonQuery();
}
using (var command = new SQLiteCommand(createPitstopTableQuery, Connection))
{
command.ExecuteNonQuery();
}
//Create the stats
string createStatsTableQuery = @"CREATE TABLE IF NOT EXISTS Stats
//Create the stats
string createStatsTableQuery = @"CREATE TABLE IF NOT EXISTS Stats
(Lap INTEGER NOT NULL,
DriverID INTEGER NOT NULL,
Tyre VARCHAR NOT NULL,
@@ -69,10 +70,96 @@ namespace Test_Merge
GapToLeader INTEGER NOT NULL,
Position INTEGER NOT NULL,
PRIMARY KEY (Lap, DriverID));";
using (var command = new SQLiteCommand(createStatsTableQuery, connection))
using (var command = new SQLiteCommand(createStatsTableQuery, Connection))
{
command.ExecuteNonQuery();
}
}
public void AddDriver(string name)
{
string insertQuery = "INSERT INTO Drivers (Name) VALUES (@name);";
using (var command = new SQLiteCommand(insertQuery,Connection))
{
command.Parameters.AddWithValue("@Name",name);
try
{
command.ExecuteNonQuery();
}
catch
{
//MessageBox.Show("An error has occured while trying to insert a new driver into de Database");
}
}
}
private int GetDriverID(string name)
{
string selectQuery = "SELECT ID FROM Drivers where Name LIKE @driverName";
int result = 0;
using (var command = new SQLiteCommand(selectQuery,Connection))
{
command.Parameters.AddWithValue("@driverName",name);
try
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
result = reader.GetInt32(0);
}
}
}
catch
{
//MessageBox.Show("There has been an error while trying to retrieve the ID of a Driver from the database");
}
}
return result;
}
public void AddPitstop(string driverName,int lap,string tyre)
{
string insertQuery = "INSERT INTO Pitstops (Lap,DriverID,Tyre) VALUES (@Lap,@DriverID,@Tyre)";
using (var command = new SQLiteCommand(insertQuery,Connection))
{
command.Parameters.AddWithValue("@Lap",lap);
command.Parameters.AddWithValue("@DriverID",GetDriverID(driverName));
command.Parameters.AddWithValue("@Tyre",tyre);
try
{
command.ExecuteNonQuery();
}
catch
{
//MessageBox.Show("An error has occured while trying to insert a new pitstop into the DB" + Environment.NewLine + "Request :"+ command.ToString());
}
}
}
public void AddDriverStat(DriverData data,int lap)
{
string insertQuery = "INSERT INTO Stats (Lap,DriverID,Tyre,LapTime,Sector1,Sector2,Sector3,GapToLeader,Position) VALUES (@Lap,@DriverID,@Tyre,@LapTime,@Sector1,@Sector2,@Sector3,@GapToLeader,@Position);";
using (var command = new SQLiteCommand(insertQuery,Connection))
{
command.Parameters.AddWithValue("@Lap",lap);
command.Parameters.AddWithValue("@DriverID",GetDriverID(data.Name));
command.Parameters.AddWithValue("@Tyre",data.CurrentTyre.Coumpound.ToString());
command.Parameters.AddWithValue("@LapTime",data.LapTime);
command.Parameters.AddWithValue("@Sector1",data.Sector1);
command.Parameters.AddWithValue("@Sector2", data.Sector2);
command.Parameters.AddWithValue("@Sector3", data.Sector3);
command.Parameters.AddWithValue("@GapToLeader", data.GapToLeader);
command.Parameters.AddWithValue("@Position", data.Position);
try
{
command.ExecuteNonQuery();
}
catch
{
//MessageBox.Show("An error has occured while trying to insert infos about a driver");
}
}
}
}