Rust Postgres实例代码
<p>Rust Postgres是一个纯Rust实现的PostgreSQL客户端库,无需依赖任何外部二进制文件。这意味着它可以轻松集成到你的Rust项目中,提供对PostgreSQL的支持。</p><p>特点</p>
<ul><li><strong>高性能</strong>:Rust Postgres提供了高性能的数据库交互功能,这对于需要处理大量数据的应用来说是非常重要的。</li></ul>
<ul><li><strong>安全性</strong>:由于Rust本身的设计就注重安全性,因此Rust Postgres也继承了这一特性,能够在编译期间检测并预防大部分潜在的安全问题。</li></ul>
<ul><li><strong>易用性</strong>:Rust Postgres的API设计简洁明了,易于理解和使用,这使得开发者能够快速上手并开始使用。</li></ul>
<p>目录结构</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202405/20245784636831.png" /></p>
<p>cargo.toml配置文件</p>
<div class="jb51code"><pre class="brush:plain;">
name = "MyPostgres"
version = "0.1.0"
edition = "2021"
postgres = "0.19.7"
[]
name = "createPostgresDatabase"
path = "examples/SQL/createPostgresDatabase.rs"
doc-scrape-examples = true
name = "Create Postgres Database"
description = "demonstrates postgreSQL database create"
category = "SQL Rendering"
wasm = true</pre></div>
<p>假设已有数据库library,初始数据库,可在官方下载软件,按照过程设置密码,本实例中是123456。<br />postgreSQL不允许管理员登录命令行,可以在路径.\PostgreSQL\16\pgAdmin 4\runtime找到pgAdmin4.exe打开可视化界面。</p>
<p>PostgreSQL: Windows installers</p>
<p>执行文件createPostgresDatabase.rs<br />增删改查</p>
<div class="jb51code"><pre class="brush:plain;">use postgres::{Client, NoTls, Error};
use std::collections::HashMap;
use std::rc::Rc;
use std::cell::RefCell;
struct Author {
_id: i32,
name: String,
country: String
}
struct Nation {
nationality: String,
count: i64,
}
fn establish_client() -> Client {
Client::connect
("postgresql://postgres:123456@localhost/library", NoTls).expect("REASON")
}
fn main() -> Result<(), Error> {
// let mut client =
// Client::connect("postgresql://postgres:123456@localhost/library", NoTls)?;
letclient =Rc::new(RefCell::new(establish_client()));
client.clone().borrow_mut().batch_execute("
CREATE TABLE IF NOT EXISTS author (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
country VARCHAR NOT NULL
)
")?;
client.clone().borrow_mut().batch_execute("
CREATE TABLE IF NOT EXISTS book(
id SERIAL PRIMARY KEY,
title VARCHAR NOT NULL,
author_id INTEGER NOT NULL REFERENCES author
)
")?;
let mut authors = HashMap::new();
authors.insert(String::from("Chinua Achebe"), "Nigeria");
authors.insert(String::from("Rabindranath Tagore"), "India");
authors.insert(String::from("Anita Nair"), "India");
for (key, value) in &authors {
let author = Author {
_id: 0,
name: key.to_string(),
country: value.to_string()
};
client.clone().borrow_mut().execute(
"INSERT INTO author (name, country) VALUES ($1, $2)",
&[&author.name, &author.country],
)?;
}
for row inclient.clone().borrow_mut().query("SELECT id, name, country FROM author", &[])? {
let author = Author {
_id: row.get(0),
name: row.get(1),
country: row.get(2),
};
println!("Author {} is from {}", author.name, author.country);
}
client.clone().borrow_mut().execute("DROP TABLE book",&[]);
// let result =
// client.clone().borrow_mut().execute
// ("DELETE FROM author WHERE id >= $1 AND id <= $2", &[&1i32, &100i32])?;
//
// println!("{:?}",result);
let a2 = Author {
_id: 0,
name: "YinThunder".to_string(),
country: "1".to_string()
};
let result =
client.clone().borrow_mut().execute
("UPDATEauthor SET name = $1 WHERE id >= $2 AND id <= $3", &[&a2.name,&1i32, &100i32])?;
println!("{:?}",result);
selectDataTable(client.clone());
droptDataTable(client.clone());
Ok(())
}
fn selectDataTable(client: Rc<RefCell<Client>>) ->Result<(), Error>{
for row in client.borrow_mut().query("SELECT id, name, country FROM author", &[])? {
let author = Author {
_id: row.get(0),
name: row.get(1),
country: row.get(2),
};
println!("Author {} is from {}", author.name, author.country);
}
Ok(())
}
fn droptDataTable(client: Rc<RefCell<Client>>) ->Result<(), Error>{
client.borrow_mut().execute("DROP TABLE author",&[]);
Ok(())
}
</pre></div>
<p>命令行执行指令</p>
<div class="jb51code"><pre class="brush:plain;">cargo run --example createPostgresDatabase</pre></div>
<p>到此这篇关于Rust Postgres实例的文章就介绍到这了,更多相关Rust Postgres实例内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>Rust 连接 PostgreSQL 数据库的详细过程</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]