我希望此代码从文件夹导入 .log-Files 一次并将其写入 SQL-Lite 数据库。 这是一个控制台应用程序。
如果您使用 /imp ,它应该查看文件夹中是否有任何 .log 文件并将 .log 文件的内容导入到 SQLite 表中。 此外,它将 .log 文件的名称保存在另一个表中。 它这样做是为了识别已经导入的 .log 文件。 此步骤应防止程序两次导入同一 .log 文件的内容。
不过好像没有效果。
例如:
我的文件夹中有 2 个 .log 文件 --> test1.log 和 test2.log
我第一次使用 /imp 运行程序 --> 2 个 .log 文件的内容存储在一个表中,.log 文件的名称存储在另一个表中。
我第二次使用 /imp 运行程序 --> 2 个 .log 文件的内容再次存储在表中(因此它是以前的两倍),并且 .log 文件的名称不会再次存储在另一个表中(这很好,因为它们已经在表中了)
using System.Data.SQLite;
using System.IO;
class Program {
static string connectionString = "Data Source=realestate.db";
static void Main(string[] args) {
if (args.Length == 0) {
Console.WriteLine(
"Invalid program invocation. Use /n for name output or /imp for import.");
return;
}
using (SQLiteConnection connection =
new SQLiteConnection(connectionString)) {
connection.Open();
Console.WriteLine("Database connection opened successfully.");
string command = args[0].ToLower();
switch (command) {
case "/n":
PrintNames();
break;
case "/imp":
if (args.Length == 1) {
ImportAllLogFiles(connection);
} else {
string logFile = args[1];
ImportLogFile(connection, logFile);
}
break;
default:
Console.WriteLine("Invalid command.");
break;
}
}
}
static void PrintNames() {
Console.WriteLine("Marcel Wulff, René Tilinski");
}
static void ImportAllLogFiles(SQLiteConnection connection) {
string[] logFiles =
Directory.GetFiles(Directory.GetCurrentDirectory(), "*.log");
Console.WriteLine($"Found {logFiles.Length} log files.");
foreach (string logFile in logFiles) {
Console.WriteLine($"Processing log file: {logFile}");
ImportLogFile(connection, logFile);
}
}
static void ImportLogFile(SQLiteConnection connection, string logFile) {
Console.WriteLine($"Importing log file: {logFile}");
if (!File.Exists(logFile)) {
Console.WriteLine($"The log file {logFile} does not exist.");
return;
}
using (SQLiteConnection localConnection =
new SQLiteConnection(connection.ConnectionString)) {
localConnection.Open();
Console.WriteLine("Local database connection opened successfully.");
using (SQLiteCommand command = localConnection.CreateCommand()) {
command.CommandText =
@"
CREATE TABLE IF NOT EXISTS LogEntries (
Id INTEGER PRIMARY KEY,
IPAddress TEXT,
Timestamp TEXT,
Request TEXT,
StatusCode INTEGER,
BytesSent INTEGER
)";
command.ExecuteNonQuery();
Console.WriteLine(
"LogEntries table created or already exists.");
// Add the command to create ImportedLogFiles table
command.CommandText =
@"
CREATE TABLE IF NOT EXISTS ImportedLogFiles (
Id INTEGER PRIMARY KEY,
FileName TEXT UNIQUE
)";
command.ExecuteNonQuery();
Console.WriteLine(
"ImportedLogFiles table created or already exists.");
}
ImportLogEntries(localConnection, logFile);
MarkLogFileAsImported(localConnection, logFile);
}
Console.WriteLine($"{logFile} successfully imported!");
}
static void ImportLogEntries(SQLiteConnection connection, string logFile) {
using (SQLiteCommand command = connection.CreateCommand()) {
command.CommandText =
@"
INSERT INTO LogEntries (IPAddress, Timestamp, Request, StatusCode, BytesSent)
VALUES (@ip, @timestamp, @request, @statusCode, @bytesSent)";
using (StreamReader sr = new StreamReader(logFile)) {
string line;
while ((line = sr.ReadLine()) != null) {
try {
Console.WriteLine($"Processing line: {line}");
string[] parts = line.Split(' ');
string ip = parts[0];
string timestamp =
parts[3] + " " + parts[4].Trim('[', ']');
string request =
parts[5] + " " + parts[6] + " " + parts[7];
int statusCode = int.Parse(parts[8]);
int bytesSent = int.Parse(parts[9]);
command.Parameters.AddWithValue("@ip", ip);
command.Parameters.AddWithValue("@timestamp",
timestamp);
command.Parameters.AddWithValue("@request", request);
command.Parameters.AddWithValue("@statusCode",
statusCode);
command.Parameters.AddWithValue("@bytesSent",
bytesSent);
Console.WriteLine(
$"Executing command: {command.CommandText}");
command.ExecuteNonQuery();
Console.WriteLine($"Line processed successfully.");
} catch (Exception ex) {
Console.WriteLine(
$"Error parsing line: {line}. Error: {ex.Message}");
} finally {
command.Parameters.Clear();
}
}
}
}
}
static bool IsLogFileImported(SQLiteConnection connection, string logFile) {
using (SQLiteCommand command = connection.CreateCommand()) {
command.CommandText =
"SELECT COUNT(*) FROM ImportedLogFiles WHERE FileName = @fileName";
command.Parameters.AddWithValue("@fileName", logFile);
int count = Convert.ToInt32(command.ExecuteScalar());
return count > 0;
}
}
static void MarkLogFileAsImported(SQLiteConnection connection,
string logFile) {
using (SQLiteCommand command = connection.CreateCommand()) {
command.CommandText =
"INSERT OR IGNORE INTO ImportedLogFiles (FileName) VALUES (@fileName)";
command.Parameters.AddWithValue("@fileName", logFile);
try {
command.ExecuteNonQuery();
Console.WriteLine($"Marked log file as imported: {logFile}");
} catch (SQLiteException ex) {
Console.WriteLine(
$"Error marking log file as imported: {ex.Message}");
}
}
}
}```
您能确认一下sqllite的版本吗?