Dup Ver Goto 📝

Basics

PT2/lang/rust/mysql does not exist
To
36 lines, 129 words, 1011 chars Page 'Basics' does not exist.

exec_drop and query_drop

If you want to pass parameters, use exec_drop, else query_drop. Use the _drop versions if you don't want to use the returned value.

use mysql::*;
use mysql::prelude::*;

fn main() -> std::result::Result<(),Box<dyn std::error::Error>> {
    let url = "mysql://mrflibble:hexvision@localhost/fileindex";
    let pool = Pool::new(url)?;
    let mut conn = pool.get_conn()?;

    println!("Connected");

    // Note query_drop, not query.
    // If query, then there is a value returned,
    // and Rust can't infer the type of it,
    // so conn.query(..) or conn.exec(..)
    // will give an error.
    conn.query_drop(
        r"DROP TABLE IF EXISTS hello;"
        );
    conn.query_drop(
        r"CREATE TABLE hello (
            id INTEGER AUTO_INCREMENT PRIMARY KEY,
            val INTEGER NOT NULL
        )")?;

    for n in 1..10 {
        conn.exec_drop(r"INSERT INTO hello (val)
            VALUES (:n)", params! { "n" => n as i32});
    }

    Ok(())
}