其他分享
首页 > 其他分享> > rust postgres

rust postgres

作者:互联网

use postgres::{Client, NoTls};

fn main(){
    let mut client = Client::connect("host=localhost user=postgres password=postgres port=5433", NoTls).unwrap();

    client.batch_execute("
        CREATE TABLE person (
            id      SERIAL PRIMARY KEY,
            name    TEXT NOT NULL,
            data    BYTEA
        )
    ").unwrap();
    
    let name = "Ferris";
    let data = None::<&[u8]>;
    client.execute(
        "INSERT INTO person (name, data) VALUES ($1, $2)",
        &[&name, &data],
    ).unwrap();
    
    for row in client.query("SELECT id, name, data FROM person", &[]).unwrap() {
        let id: i32 = row.get(0);
        let name: &str = row.get(1);
        let data: Option<&[u8]> = row.get(2);
    
        println!("found person: {} {} {:?}", id, name, data);
    }
    
}

  

标签:postgres,unwrap,name,let,data,id,rust,row
来源: https://www.cnblogs.com/pythonClub/p/16524570.html