banner
[面包]MrTwoC

[面包]MrTwoC

你好,欢迎来到这个基于区块链的个人博客 名字:面包 / MrTwoc 爱好:跑步(5/10KM)、拿铁咖啡、游戏(Minecraft、Warframe、CS) 兴趣方向:Rust、区块链、图形学、游戏设计与开发
bilibili
steam
email
github

2. Movie Information Maintenance (Command Line Version)

In the first part of the project code, the management of movie information in the JSON file has been implemented, and the addition and deletion operations of movies are restricted through role-based access.

This time, the code has been modified to change the project to a workspace, and the JSON file storing movies has been changed to SQLite storage, allowing for CRUD operations using the database, as well as role determination using the database.

The specific code section is replaced by db_services.rs instead of the original services.rs. It includes operations for adding, deleting, and modifying movie information, as well as user role identification functionality.

Additionally, the sql_tools.rs part implements the database initialization operation, adding a few test data entries when creating the database, and includes a function to read and transfer the JSON file to the database.

// db_tools.rs
// Transfer all movie information from the JSON file to the database
fn json_save_to_db(conn: &Connection) -> Result<(), Box<dyn Error>> {
    let movies = read_form_json()?;
    for movie in movies {
        conn.execute(
            "INSERT INTO movies (disc, year, title, remark, user_id) VALUES (?1, ?2, ?3, ?4, ?5)",
            (movie.disc, movie.year, movie.title, movie.remark, 1),
        )?;
    }
    Ok(())
}

The complete code can be found in the GitHub repository.

Currently, I am a beginner in Rust and feel that my code is ugly, and I haven't utilized Rust's struct features. There is still room for improvement in the future.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.