Read/Write to SQLite

This commit is contained in:
Stedd 2021-08-01 16:12:59 +02:00
parent c9122ab361
commit 154c21fdcd
7 changed files with 106 additions and 29 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<configuration> <configuration>
<connectionStrings> <connectionStrings>
<add name="Defaule" connectionString="Data Source = ./data.db;version=3" providerName="System.Data.SqlClient"/> <add name="Default" connectionString="Data Source = ./data.db;version=3" providerName="System.Data.SqlClient"/>
</connectionStrings> </connectionStrings>
</configuration> </configuration>

View File

@ -7,7 +7,14 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Npgsql" Version="5.0.7" /> <PackageReference Include="Dapper" Version="2.0.90" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.114.3" />
</ItemGroup>
<ItemGroup>
<None Update="data.db">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -7,7 +7,8 @@
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800">
<Grid> <Grid>
<Button x:Name="saveButton" Content="Save" HorizontalAlignment="Left" Margin="400,0,0,173" VerticalAlignment="Bottom" Height="216" Width="349" Click="saveButton_Click"/> <Button x:Name="saveButton" Content="Save" HorizontalAlignment="Left" Margin="536,0,0,309" VerticalAlignment="Bottom" Height="80" Width="180" Click="saveButton_Click" FontSize="20"/>
<Button x:Name="readButton" Content="Read" Margin="536,0,84,0" Click="readButton_click" FontSize="20" Height="80" Width="180" VerticalAlignment="Center"/>
</Grid> </Grid>
</Window> </Window>

View File

@ -14,50 +14,51 @@ using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Text.Json;
namespace CryptoCalc namespace CryptoCalc
{ {
/// <summary> /// <summary>
/// Interaction logic for MainWindow.xaml /// Interaction logic for MainWindow.xaml
/// </summary> /// </summary>
///
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private readonly Random rand = new(); private readonly Random rand = new();
private readonly JsonSerializerOptions jsonOptions = new(); private List<Transaction> transactions = new();
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
jsonOptions.WriteIndented = true;
} }
private void saveButton_Click(object sender, RoutedEventArgs e) private void saveButton_Click(object sender, RoutedEventArgs e)
{ {
Debug.WriteLine("asdf"); Transaction t = new();
t.Date_Year = DateTime.Today.Year;
t.Date_Month = DateTime.Today.Month;
t.Date_Day = DateTime.Today.Day;
t.Time_Hour = DateTime.Today.Hour;
t.Time_Minute = DateTime.Today.Minute;
t.Time_Second = DateTime.Today.Second;
t.DateTimeString = DateTime.Now.ToString();
t.CryptoCurrency = "ETH";
t.Amount = (float)rand.NextDouble();
t.TransactionType = "WITHDRAWAL";
t.Service = "MiraiEx";
t.Comment = "Test";
var weatherForecast = new WeatherForecast SqliteDataAccess.SaveTransaction(t);
}
private void readButton_click(object sender, RoutedEventArgs e)
{ {
Date = DateTime.Parse("2019-08-01"), transactions = SqliteDataAccess.LoadTransactions();
TemperatureCelsius = rand.Next(0,100), foreach (var x in transactions)
Summary = "Hot"
};
string fileName = "asdf.json";
string jsonString = JsonSerializer.Serialize(weatherForecast, jsonOptions);
File.AppendAllText(fileName, jsonString);
Debug.WriteLine(File.ReadAllText(fileName));
}
}
public class WeatherForecast
{ {
public DateTimeOffset Date { get; set; } Debug.WriteLine(x.Date_Year);
public int TemperatureCelsius { get; set; } }
public string Summary { get; set; } //Debug.WriteLine(transactions.Date_Year.ToString());
}
} }
} }

41
SqliteDataAccess.cs Normal file
View File

@ -0,0 +1,41 @@
using Dapper;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CryptoCalc
{
class SqliteDataAccess
{
public static List<Transaction> LoadTransactions()
{
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
var output = cnn.Query<Transaction>("select Date_Year from RawData", new DynamicParameters());
Debug.WriteLine("Loaded DB data");
return output.ToList();
}
}
public static void SaveTransaction(Transaction transaction)
{
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
cnn.Execute("insert into RawData (Date_Year, Date_Month, Date_Day, Time_Hour, Time_Minute, Time_Second, DateTimeString, CryptoCurrency, Amount, TransactionType, Service, Comment) values (@Date_Year, @Date_Month, @Date_Day, @Time_Hour, @Time_Minute, @Time_Second, @DateTimeString, @CryptoCurrency, @Amount, @TransactionType, @Service, @Comment)", transaction);
Debug.WriteLine("Saved DB data");
}
}
private static string LoadConnectionString(string id = "Default")
{
return ConfigurationManager.ConnectionStrings[id].ConnectionString;
}
}
}

27
Transaction.cs Normal file
View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CryptoCalc
{
class Transaction
{
public int Index { get; set; }
public int Date_Year { get; set; }
public int Date_Month { get; set; }
public int Date_Day { get; set; }
public int Time_Hour { get; set; }
public int Time_Minute { get; set; }
public int Time_Second { get; set; }
public string DateTimeString { get; set; }
public string CryptoCurrency { get; set; }
public float Amount { get; set; }
public string TransactionType { get; set; }
public string Service{ get; set; }
public string Comment { get; set; }
}
}

BIN
data.db

Binary file not shown.