分享好友 编程语言首页 频道列表

Rust 连接 PostgreSQL 数据库

rust文章/教程  2023-02-09 09:070

这次,我们使用 postgres 这个 crate 来连接和操作 PostgreSQL 数据库。

创建好项目后,在 cargo.toml 里添加 postgres 的依赖:

7 
8 
9 
10 
[ dependenci es] 
postgres " 
0.19.2"

 

首先,导入相关的类型,并创建一个 Person struct:

@ main.rs 6, 
src > @ main.rs > . 
1 
2 
3 
4 
5 
6 
8 
use postgres :: {error :: Error, 
Åderive(Debug)] 
struct Person { 
id: i32, 
name: String, 
data: Option<Vec<u8>>, 
Client, 
NoT1s};

 

再创建 create_db 函数,用来创建数据库和表,它返回一个 Result,里面可能是 Client 或错误:

 

注意,Client::connect() 函数所接受的连接字符串可以是两种形式的:

10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
fn 
create_db() Result<Client, 
Error> { 
let 
let 
let 
let 
let 
let 
let 
let 
" postgres"; 
username 
password 
" postgres" ; 
host 
"localhost"; 
" 5432"; 
port 
database 
" rust2021"; 
conn_str = &format!( 
"postgres://{}{}{}ä){}{}{}{}{}", 
username , 
if password.is_empty() { 
password , 
host, 
port.is_empty() { 
if 
port , 
if database.is_empty() { 
database 
} else 
else { } , 
} else 
client = Client :: connect(conn_str, 
mut 
client.execute("DROP TABLE person" , 
NoT1s)?, 
client. execute( 
"CREATE TABLE person ( 
id 
name 
data 
81], 
Ok(client) 
SERIAL PRIMARY KEY, 
TEXT NOT NULL, 
BYTEA

  • Key-Value 形式。例如:Client::connect("host=localhost user=postgres", NoTls)?; 具体的 key 需要查阅官方文档。
  • URL 形式。本例中使用的是 URL 形式。

 

一个相对完整的数据库连接字符串 URL 格式是:

postgres://username[:password]@host[:port][/database],其中 password、port、database 都是可选的。所以上面代码中做了相应的判断处理。

 

Client::connect() 函数的第二个参数用的是 NoTls,为了简单起见,这里我们不使用 TLS。

第 30、32 行,使用 Client 的 execute 方法先删除数据表(如果存在的话),然后再创建 person 表。

最后返回 Client。

 

接下来,创建 insert_data 函数,来插入一些数据:

44 
45 
46 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
fn 
insert_data(client: &mut Client) 9 
let 
let 
Person { 
pl 
id: 
1, 
"Dave" .to_string(), 
name : 
data: None, 
Person { 
id: 
2, 
"Nick" .to_string(), 
name : 
data: None, 
client. execute( 
"INSERT INTO person (id, name, data) 
VALUES ($1, $2, $3), 
8[8p1.id, 8p1.name, 8p1.data, &p2.id, 
Error> { 
. name , 
. data],

注意该函数的参数 Client 必须是 mut 的。

 

再创建一个查询数据的函数:

66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
fn 
get_data(client: 8mut Client) 
let mut persons = Vec :: new(); 
in 
for row 
persons.push(Person { 
id: row.get(ø), 
name: row.get(l), 
data: row.get(2), 
D; 
Ok(persons) 
Result<Vec<Person> , 
id, name, data FROM 
Error> 
person"

这里,我们直接对 Client 的 query 方法返回的结果进行遍历,最后方法返回一个 Vec。

 

最后,在 main 函数里依次调用这些函数,并把查询结果打印出来:

79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
fn 
main() Result<(), Error> { 
create_db()?; 
let mut 
client 
insert_data(8mut client)? ; 
get_data(&mut client)? 
let 
persons 
persons { 
for p 
print In! ("Person: { : ? } ", 
p);

 

结果如下:

$ cargo run 
pgdb (D:\Projects\Rust\pgdb) 
Compiling 
dev [unoptimized + debuginfo] target(s) in 1.30s 
Finished 
'target\debug\pgdb. exe 
Running 
Person: Person { id: 1, name: "Dave" 
data: None } 
Person: Person { id: 2, name: "Nick", data: None }

 

 

全部代码如下: 

use postgres::{error::Error, Client, NoTls};

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>,
}

fn create_db() -> Result<Client, Error> {
    let username = "postgres";
    let password = "postgres";
    let host = "localhost";
    let port = "5432";
    let database = "rust2021";

    let conn_str = &format!(
        "postgres://{}{}{}@{}{}{}{}{}",
        username,
        if password.is_empty() { "" } else { ":" },
        password,
        host,
        if port.is_empty() { "" } else { ":" },
        port,
        if database.is_empty() { "" } else { "/" },
        database
    );
    let mut client = Client::connect(conn_str, NoTls)?;

    let _ = client.execute("DROP TABLE person", &[]);

    client.execute(
        "CREATE TABLE person (
        id      SERIAL PRIMARY KEY,
        name    TEXT NOT NULL,
        data    BYTEA
    )",
        &[],
    )?;

    Ok(client)
}

fn insert_data(client: &mut Client) -> Result<(), Error> {
    let p1 = Person {
        id: 1,
        name: "Dave".to_string(),
        data: None,
    };
    let p2 = Person {
        id: 2,
        name: "Nick".to_string(),
        data: None,
    };

    client.execute(
        "INSERT INTO person (id, name, data)
    VALUES ($1, $2, $3),
    ($4, $5, $6)",
        &[&p1.id, &p1.name, &p1.data, &p2.id, &p2.name, &p2.data],
    )?;

    Ok(())
}

fn get_data(client: &mut Client) -> Result<Vec<Person>, Error> {
    let mut persons = Vec::new();
    for row in client.query("SELECT id, name, data FROM person", &[])? {
        persons.push(Person {
            id: row.get(0),
            name: row.get(1),
            data: row.get(2),
        });
    }

    Ok(persons)
}

fn main() -> Result<(), Error> {
    let mut client = create_db()?;
    insert_data(&mut client)?;

    let persons = get_data(&mut client)?;

    for p in persons {
        println!("Person: {:?}", p);
    }

    Ok(())
}

  

查看更多关于【rust文章/教程】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Rust到底值不值得学--Rust对比、特色和理念
前言其实我一直弄不明白一点,那就是计算机技术的发展,是让这个世界变得简单了,还是变得更复杂了。当然这只是一个玩笑,可别把这个问题当真。然而对于IT从业者来说,这可不是一个玩笑。几乎每一次的技术发展,都让这个生态变得更为复杂。“英年早秃”已经成

0评论2023-03-08818

全栈程序员的新玩具Rust(三)板条箱
上次用到了stdout,这次我们来写一个更复杂一点的游戏rust的标准库叫做std,默认就会引入。这次我们要用到一个随机数函数,而随机数比较尴尬的一点是这玩意不在标准库中,我们要额外依赖一个库。很多编程方案都有自己的模块化库系统,rust也不例外,不过rust

0评论2023-02-10729

【Rust】标准库-Result rust数据库
环境Rust 1.56.1VSCode 1.61.2概念参考:https://doc.rust-lang.org/stable/rust-by-example/std/result.html示例main.rsmod checked {#[derive(Debug)]pub enum MathError {DivisionByZero,NonPositiveLogarithm,NegativeSquareRoot,}pub type MathResult =

0评论2023-02-09978

【Rust】标准库-引用 rust 数据库框架
环境Rust 1.56.1VSCode 1.61.2概念参考:https://doc.rust-lang.org/stable/rust-by-example/std/rc.html示例rust 使用 Rc 来实现引用计数。main.rsuse std::rc::Rc;fn main() {let rc_examples = "Rc examples".to_string();{println!("--- rc_a is created

0评论2023-02-09638

rust 打印当前时间
let now = time::now();let f_now = time::strftime("%Y-%m-%dT%H:%M:%S", now).unwrap();println!("now: {:?}", f_now);

0评论2023-02-09689

【Rust】线程 rust编程语言
环境Rust 1.56.1VSCode 1.61.2概念参考:https://doc.rust-lang.org/stable/rust-by-example/std_misc/threads.html示例main.rsuse std::thread;const N_THREADS: u32 = 10;fn main() {let mut children = vec![];for i in 0..N_THREADS {children.push(threa

0评论2023-02-09956

更多推荐