Clean up db more.

This commit is contained in:
mattbk
2025-10-04 22:46:44 -05:00
parent c228129e21
commit 97f6a4bdb9

View File

@ -5,8 +5,9 @@ use std::time::{SystemTime, UNIX_EPOCH};
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
// Set up db and make sure it works
let _ = create_database(); let _ = create_database();
let _ = insert_packet("foo", 1); let _ = insert_packet("foo");
let socket_result: Result<UdpSocket, std::io::Error> = UdpSocket::bind("127.0.0.1:7878"); let socket_result: Result<UdpSocket, std::io::Error> = UdpSocket::bind("127.0.0.1:7878");
let socket: UdpSocket = match socket_result { let socket: UdpSocket = match socket_result {
@ -32,7 +33,7 @@ fn main() -> std::io::Result<()> {
String::from_utf8_lossy(&buffer[..bytes_received]) String::from_utf8_lossy(&buffer[..bytes_received])
); );
let _ = insert_packet(&String::from_utf8_lossy(&buffer[..bytes_received]), 1); let _ = insert_packet(&String::from_utf8_lossy(&buffer[..bytes_received]));
// Echo the data back to the client // Echo the data back to the client
socket.send_to(&buffer[..bytes_received], src_addr)?; socket.send_to(&buffer[..bytes_received], src_addr)?;
@ -50,7 +51,6 @@ fn create_database() -> Result<()> {
"CREATE TABLE IF NOT EXISTS packets ( "CREATE TABLE IF NOT EXISTS packets (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
raw TEXT NOT NULL, raw TEXT NOT NULL,
age INTEGER NOT NULL,
date INTEGER NOT NULL date INTEGER NOT NULL
)", )",
[], // No parameters needed [], // No parameters needed
@ -60,18 +60,18 @@ fn create_database() -> Result<()> {
Ok(()) Ok(())
} }
fn insert_packet(raw: &str, age: i32) -> Result<()> { fn insert_packet(raw: &str) -> Result<()> {
let conn = Connection::open("pkt_database.db")?; let conn = Connection::open("pkt_database.db")?;
let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
// Insert // Insert
conn.execute( conn.execute(
"INSERT INTO packets (raw, age, date) VALUES (?1, ?2, ?3)", "INSERT INTO packets (raw, date) VALUES (?1, ?2)",
params![raw, age, ds], // Bind parameters params![raw, ds], // Bind parameters
)?; )?;
println!("Row inserted successfully."); //println!("Row inserted successfully.");
Ok(()) Ok(())
} }