Upload files to "PlaceablesDBA"

This commit is contained in:
LockOn 2024-05-22 00:00:17 +00:00
commit 2037890c61

View File

@ -0,0 +1,149 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
//using MySql.Data.MySqlClient;
class Program
{
static void Main(string[] args)
{
// Replace "path_to_your_file.txt" with the actual path to your text file
string filePath = "C:\\Users\\Freri\\Desktop\\Placeables\\placeables.2da";
try
{
string[] lines = File.ReadAllLines(filePath);
if (lines.Length < 3)
{
Console.WriteLine("Invalid file format. File must contain at least 3 lines.");
return;
}
// Extract column names and field lengths from the third line
Dictionary<string, int> columnFieldLengths = ExtractColumnFieldLengths(lines[2]);
// Create the CREATE TABLE command
string createTableCommand = "CREATE TABLE IF NOT EXISTS Placables_2da (";
createTableCommand += "Plcb_ID BIGINT(10), "; // First field
foreach (var column in columnFieldLengths)
{
createTableCommand += $"{column.Key} VARCHAR({column.Value}), ";
}
createTableCommand = createTableCommand.TrimEnd(',', ' ') + ");";
Console.WriteLine(createTableCommand);
// Assuming you have a MySQL connection established
/* using (MySqlConnection connection = new MySqlConnection("YourConnectionString"))
{
connection.Open();
// Create a MySqlCommand object
MySqlCommand command = new MySqlCommand();
// Set the connection for the command
command.Connection = connection;
// Execute the CREATE TABLE command
command.CommandText = createTableCommand;
command.ExecuteNonQuery();
*/
// Populate the table with data
for (int i = 3; i < lines.Length; i++)
{
string[] values = ParseLineValues(lines[i]);
// Prepare the INSERT statement
StringBuilder insertCommand = new StringBuilder($"INSERT INTO YourTableName (");
foreach (var column in columnFieldLengths)
{
insertCommand.Append($"{column.Key}, ");
}
insertCommand = insertCommand.Replace(", ", ") VALUES (", insertCommand.Length - 2, 2).Append(")");
for (int j = 0; j < values.Length; j++)
{
insertCommand.Append($"'{values[j]}', ");
}
insertCommand = insertCommand.Remove(insertCommand.Length - 2, 2).Append(";");
//Console.Write(insertCommand); //Debug - to print the insert statement
//return; //Debug to stop the script after writing only a single line
// Execute the INSERT statement
/*command.CommandText = insertCommand.ToString();
command.ExecuteNonQuery();*/
}
//}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
static Dictionary<string, int> ExtractColumnFieldLengths(string line)
{
// Use regular expression to extract column names and field lengths
Dictionary<string, int> columnFieldLengths = new Dictionary<string, int>();
Regex regex = new Regex(@"\b[A-Za-z]+\b");
MatchCollection matches = regex.Matches(line);
foreach (Match match in matches)
{
string columnName = match.Value;
int fieldLength = match.NextMatch().Index - match.Index;
if(match.NextMatch().Index == 0)
{
fieldLength = 11;
}
columnFieldLengths.Add(columnName, fieldLength);
}
return columnFieldLengths;
}
static string[] ParseLineValues(string line)
{
List<string> values = new List<string>();
bool inQuotes = false;
string currentVal = "";
foreach (char c in line)
{
if (c == '"')
{
inQuotes = !inQuotes;
currentVal += c; // Include the quote in the value
}
else if (c == ' ' && !inQuotes)
{
if (!string.IsNullOrEmpty(currentVal))
{
values.Add(currentVal);
currentVal = "";
}
}
else
{
currentVal += c;
}
}
if (!string.IsNullOrEmpty(currentVal))
{
values.Add($"\"{currentVal}\""); // Add double quotes to the beginning and end of the value
}
return values.ToArray();
}
}