掌握Rust编程:PostgreSQL入门指南

发表时间: 2023-06-09 03:31
[dependencies]postgres = "0.19"
// postgres_demo/src/main.rsuse postgres::{Client, NoTls};const DROP_TABLE: &str = "DROP TABLE IF EXISTS books";const CONNECTION: &str = "postgres://postgres:postgres@localhost:5432";const CREATE_TABLE: &str = "CREATE TABLE IF NOT EXISTS books                             (id SERIAL PRIMARY KEY,                            title VARCHAR NOT NULL,                            author VARCHAR NOT NULL,                            year SERIAL)";fn reset_db(conn: &mut Client) {    let _ = conn.execute(DROP_TABLE, &[]).unwrap();    let _ = conn.execute(CREATE_TABLE, &[]).unwrap();}#[derive(Debug)]struct Book {    id: i32,    title: String,    author: String,    year: i32}fn main() {    let mut conn = Client::connect(CONNECTION, NoTls).unwrap();    reset_db(&mut conn);    let book = Book {        id: 3,        title: "A programmers introduction to mathematics".to_string(),        author: "Dr. Jeremy Kun".to_string(),        year: 2018    };    conn.execute("INSERT INTO books (id, title, author, year) VALUES (, , , )",                 &[&book.id, &book.title, &book.author, &book.year]).unwrap();    for row in &conn.query("SELECT id, title, author, year FROM books", &[]).unwrap() {        let book = Book {            id: row.get(0),            title: row.get(1),            author: row.get(2),            year: row.get(3)        };        println!("{:?}", book);    }}