Secure crud gtk gui mariadb with rust

[dependencies]
gtk = "0.9"
mysql = "20.0"
rpassword = "5.0"
use mysql::*;
use mysql::prelude::*;

fn establish_connection() -> PooledConn {
    let url = "mysql://username:password@localhost:3306/database_name";
    let pool = Pool::new(url).expect("Failed to create a database pool");
    pool.get_conn().expect("Failed to get a connection")
}
fn create_record(conn: &mut PooledConn, name: &str, age: i32) {
    conn.exec_drop(
        "INSERT INTO users (name, age) VALUES (:name, :age)",
        params! {
            "name" => name,
            "age" => age,
        },
    ).expect("Failed to insert record");
}

fn read_records(conn: &mut PooledConn) -> Vec<(u32, String, i32)> {
    conn.query_map(
        "SELECT id, name, age FROM users",
        |(id, name, age)| (id, name, age),
    ).expect("Failed to read records")
}

fn update_record(conn: &mut PooledConn, id: u32, name: &str, age: i32) {
    conn.exec_drop(
        "UPDATE users SET name=:name, age=:age WHERE id=:id",
        params! {
            "id" => id,
            "name" => name,
            "age" => age,
        },
    ).expect("Failed to update record");
}

fn delete_record(conn: &mut PooledConn, id: u32) {
    conn.exec_drop(
        "DELETE FROM users WHERE id=:id",
        params! {
            "id" => id,
        },
    ).expect("Failed to delete record");
}
use rpassword::read_password;

fn get_secure_input(prompt: &str) -> String {
    println!("{}", prompt);
    read_password().expect("Failed to read password")
}
extern crate gtk;
use gtk::prelude::*;
use gtk::{Button, Entry, Label};

fn main() {
    gtk::init().expect("Failed to initialize GTK.");

    let window = gtk::Window::new(gtk::WindowType::Toplevel);
    window.set_title("CRUD App");
    window.set_default_size(300, 200);

    let entry_name = Entry::new();
    let entry_age = Entry::new();
    let button = Button::new_with_label("Add Record");
    let label = Label::new(None);

    button.connect_clicked(move |_| {
        let name = entry_name.get_text().unwrap();
        let age: i32 = entry_age.get_text().unwrap().parse().unwrap();
        // Assume conn is an established connection
        create_record(&mut establish_connection(), &name, age);
        label.set_text("Record added successfully!");
    });

    let vbox = gtk::Box::new(gtk::Orientation::Vertical, 5);
    vbox.pack_start(&entry_name, true, true, 0);
    vbox.pack_start(&entry_age, true, true, 0);
    vbox.pack_start(&button, true, true, 0);
    vbox.pack_start(&label, true, true, 0);

    window.add(&vbox);
    window.show_all();

    window.connect_delete_event(|_, _| {
        gtk::main_quit();
        Inhibit(false)
    });

    gtk::main();
}

Hi there, I’ve fixed the formatting for your posts so far. Please try to learn to write more properly formatted posts in the future. When the editor is in markdown mode, there’s a preview on the right-hand side.[1]

Actually, to help you avoid posting messages with broken formatting, I’ve now also clicked the toggle for you to enable the rich-text (“what you see is what you get” style) editor. It looks like you have already used the formatting toolbar anyway, this way you can mostly ignore the concerns with markdown syntax and ``` characters and use the toolbar directly directly to create code blocks / boxes.

(On mobile, the toolbar is visible through the button with a icon similar to “≡”.)

If you prefer to switch back to the markdown editor, they are easily toggled with the leftmost button in the editor toolbar, anyway :wink:


To make it any actual kind of “tutorial”, you would also need to add some actual human-readable explanations, not just post code files without much further commentary.

If you’re looking for a place to simply put small (collections of) source code files up online for yourself or to have a link to share, this forum isn’t the right place to do so – consider using something like gist.github.com instead.

If you are interested in posting tutorial content for others to read[2] you should still reduce the frequency of opening new topics here. For example: You can collect the things you would want to post, and then choose the best, or combine / group things together in 1 thread. For actual / more in-depth tutorial content, hosting the tutorial(s) on a different platform[3] could be an alternative to consider as well, and once you’re added some content and want readers to start looking into it, you can open a forum thread here with a link, to be able to get some feedback :wink:


  1. you might have to first dismiss some informational messages appearing in the preview area to see it ↩︎

  2. and make them more readable, as mentioned above, by including text with actual explanations or other commentary; perhaps even also with questions you yourself would like answered? ↩︎

  3. I’m personally not sure I know any concrete platforms to suggest myself; many more in-depth tutorials people write are on their own websites; github pages could be an easy way to create one, e.g. I was able to find description (here) of how one could set up one with mdbook-powered content ↩︎

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.