Skip to content

Instantly share code, notes, and snippets.

@wRadion
Created March 12, 2020 18:36
Show Gist options
  • Save wRadion/2f9ca5699d1c68e3b0cb4f54dbee43f6 to your computer and use it in GitHub Desktop.
Save wRadion/2f9ca5699d1c68e3b0cb4f54dbee43f6 to your computer and use it in GitHub Desktop.
Create SQLite Database C#
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Text;
using System.Windows;
namespace SQLiteTest
{
public class Text
{
public string TextSource { get; set; }
public string TextCible { get; set; }
public int LigneSource { get; set; }
public int LigneCible { get; set; }
public string Etat { get; set; }
public Text() { }
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public const string DbPath = "database.db";
public void AddRow(SQLiteCommand cmd, ICollection<Text> texts)
{
StringBuilder str = new StringBuilder("INSERT INTO texts(textsource, textcible, lignesource, lignecible, etat) VALUES");
if (texts.Count == 0) return;
foreach (Text text in texts)
str.Append($"(\"{text.TextSource}\", \"{text.TextCible}\", {text.LigneSource}, {text.LigneCible}, \"{text.Etat}\"),");
str.Remove(str.Length - 1, 1);
str.Append(';');
cmd.CommandText = str.ToString();
cmd.ExecuteNonQuery();
}
public MainWindow()
{
InitializeComponent();
string cs = "Data Source=" + DbPath;
var con = new SQLiteConnection(cs);
con.Open();
var cmd = new SQLiteCommand("", con);
cmd.CommandText = "DROP TABLE IF EXISTS texts";
cmd.ExecuteNonQuery();
cmd.CommandText = "CREATE TABLE texts(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, textsource TEXT, textcible TEXT, lignesource INT, lignecible INT, etat STRING);";
cmd.ExecuteNonQuery();
Text[] texts = new Text[]
{
new Text()
{
TextSource = "Ceci est le text source",
TextCible = "Ceci est le text cible",
LigneSource = 1,
LigneCible = 1,
Etat = "ok"
},
new Text()
{
TextSource = "Ceci est le text source",
TextCible = "Ceci est le text cible",
LigneSource = 1,
LigneCible = 1,
Etat = "ok"
},
new Text()
{
TextSource = "Ceci est le text source",
TextCible = "Ceci est le text cible",
LigneSource = 1,
LigneCible = 1,
Etat = "ok"
},
new Text()
{
TextSource = "Ceci est le text source",
TextCible = "Ceci est le text cible",
LigneSource = 1,
LigneCible = 1,
Etat = "ok"
},
new Text()
{
TextSource = "Ceci est le text source",
TextCible = "Ceci est le text cible",
LigneSource = 1,
LigneCible = 1,
Etat = "ok"
}
};
AddRow(cmd, texts);
cmd.CommandText = "SELECT COUNT(*) FROM texts";
string count = cmd.ExecuteScalar().ToString();
Console.WriteLine(count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment