with the help of AI I wrote an Code that could read txt documents and format them into an exportable csv. Now I want the program to check if the current title can be found in the my_database.db and then copy the artists from the database in the csv. My Problem is that the artists aren’t being copied in the csv. I wrote the code in Rust.
This is an example of a line in the input.txt I load in the program:
KPM_KPM_0858_00101_SCARS_A_Matt_Black_Christian_J_O_Lundberg.mp3.new.01
Then the program parses this line into:
Index: KPM_KPM_0858_00101
Title: scars a
Artists: matt black christian j o lundberg
I want to check the database because in the databse the artists are seperated with an _, so I know when a new person begins.
the complete code is on GITHUB:
https://github.com/TJ-5/GEMA_RUST
If anyone could help me out it would be great! I tried a lot of things but I think this is exactly over my programming knowledge. I am new to programming and this is the first program in RUST so please don’t roast me 🙂
Thats the function that should do the scanning and copying:
fn apply_database_info(&mut self) {
let Some(conn) = self.db_connection.as_ref() else {
info!("Keine Datenbankverbindung vorhanden. Überspringe apply_database_info().");
return;
};
// Prepare the query once
let query = r#"
SELECT kuenstler
FROM tracks
WHERE LOWER(REPLACE(titel, '_', '')) = LOWER(REPLACE(?1, '_', ''))
LIMIT 1
"#;
let mut stmt = match conn.prepare(query) {
Ok(s) => s,
Err(e) => {
info!("Fehler beim Vorbereiten der Abfrage: {}", e);
return;
}
};
// Iterate through all tracks and update artists from database
for (_filename, tracks) in self.tracks_per_file.iter_mut() {
for track in tracks.iter_mut() {
let result = stmt.query_row(params![&track.titel], |row| {
row.get::<_, String>(0)
});
match result {
Ok(db_artist) => {
// Replace spaces with underscores in the artist name
let formatted_artist = db_artist.replace(" ", "_");
info!(
"Track '{}' gefunden, Künstler von '{}' zu '{}' aktualisiert",
track.titel,
track.kuenstler,
formatted_artist
);
track.kuenstler = formatted_artist;
}
Err(_) => {
info!(
"Track '{}' nicht in der Datenbank gefunden, behalte Künstler '{}'",
track.titel,
track.kuenstler
);
}
}
}
}
}
in the csv I was expecting “matt black_christian j o lundberg” because thats the way it is written in the database. So the artists are seperated with an _. Not with a comma because that would create a problem because of the csv (Comma-separated values).
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
0