I am currently working on a program, that reads Data out of a huge (40,000 lines+) *.txt File. the lines look like this:
4,Barbarendorf,505,552,1575899232,378,0
5,Der+letzte+macht+das+Licht+aus,484,458,1576458064,5757,0
in general:
$id, $name, $x, $y, $player, $points, $rank
So the function i wrote in order to get this data into the SQLite database is the following:
void ThreadMethod()
{
string sql = "";
SQLiteConnection m_dbConnection;
m_dbConnection = new SQLiteConnection("Data Source=villages.db;Version=3;");
m_dbConnection.Open();
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
try
{
using (StreamReader sr = new StreamReader("village.txt"))
{
String line;
while ((line = File.ReadLines("village.txt").Last()) != null)
{
Regex regex = new Regex(",");
string[] substrings = regex.Split(line);
int i = 0;
string[] strVillage = new string[7];
foreach (string match in substrings)
{
strVillage[i++] = match;
}
sql = "INSERT INTO villages (villageID, villageName, xCoord, yCoord, playerName, villagePoints, villageRank) values (" + strVillage[0] + ", '" + strVillage[1] + "', " + strVillage[2] + ", " + strVillage[3] + ", '" + strVillage[4] + "', " + strVillage[5] + ", " + strVillage[6] + ")";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
var lines = System.IO.File.ReadAllLines("village.txt");
System.IO.File.WriteAllLines("village.txt", lines.Take(lines.Length - 1).ToArray());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
m_dbConnection.Close();
}
It works, but it is really slow. I hope you guys can help me to improve the performance. Best Regards!