mqsql connection in rust

main.rs
use mysql::*;
use mysql::prelude::*;

fn main() {
    let pool = mysql::Pool::new("mysql://username:password@localhost/dbname").unwrap();

    let mut conn = pool.get_conn().unwrap();

    let query = "SELECT name, age FROM users WHERE id = ?";
    let params = (1,);
    
    let query_result: QueryResult<Row> = conn.exec_first(query, params).unwrap();
    match query_result {
        Some(row) => {
            let name: String = row.get("name").unwrap();
            let age: i32 = row.get("age").unwrap();
            println!("Name: {}, Age: {}", name, age);
        },
        None => println!("User not found."),
    }
}
630 chars
22 lines

related categories

gistlibby LogSnag