Rust?Postgres實例代碼
Rust Postgres是一個純Rust實現(xiàn)的PostgreSQL客戶端庫,無需依賴任何外部二進制文件。這意味著它可以輕松集成到你的Rust項目中,提供對PostgreSQL的支持。
特點
- 高性能:Rust Postgres提供了高性能的數(shù)據(jù)庫交互功能,這對于需要處理大量數(shù)據(jù)的應(yīng)用來說是非常重要的。
- 安全性:由于Rust本身的設(shè)計就注重安全性,因此Rust Postgres也繼承了這一特性,能夠在編譯期間檢測并預(yù)防大部分潛在的安全問題。
- 易用性:Rust Postgres的API設(shè)計簡潔明了,易于理解和使用,這使得開發(fā)者能夠快速上手并開始使用。
目錄結(jié)構(gòu)
cargo.toml配置文件
[package] name = "MyPostgres" version = "0.1.0" edition = "2021" [dependencies] postgres = "0.19.7" [[example]] name = "createPostgresDatabase" path = "examples/SQL/createPostgresDatabase.rs" doc-scrape-examples = true [package.metadata.example.createPostgresDatabase] name = "Create Postgres Database" description = "demonstrates postgreSQL database create" category = "SQL Rendering" wasm = true
假設(shè)已有數(shù)據(jù)庫library,初始數(shù)據(jù)庫,可在官方下載軟件,按照過程設(shè)置密碼,本實例中是123456。
postgreSQL不允許管理員登錄命令行,可以在路徑.\PostgreSQL\16\pgAdmin 4\runtime找到pgAdmin4.exe打開可視化界面。
PostgreSQL: Windows installers
執(zhí)行文件createPostgresDatabase.rs
增刪改查
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)?; let client =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 in client.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 ("UPDATE author 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(()) }
命令行執(zhí)行指令
cargo run --example createPostgresDatabase
到此這篇關(guān)于Rust Postgres實例的文章就介紹到這了,更多相關(guān)Rust Postgres實例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Rust錯誤處理之`foo(...)?`的用法與錯誤類型轉(zhuǎn)換小結(jié)
foo(...)?語法糖為Rust的錯誤處理提供了極大的便利,通過結(jié)合map_err方法和From?trait的實現(xiàn),你可以輕松地處理不同類型的錯誤,并保持代碼的簡潔性和可讀性,這篇文章主要介紹了Rust錯誤處理:`foo(...)?`的用法與錯誤類型轉(zhuǎn)換,需要的朋友可以參考下2024-05-05Rust 標(biāo)準(zhǔn)庫的結(jié)構(gòu)及模塊路徑詳解
在 Rust 中,標(biāo)準(zhǔn)庫提供了一組核心功能,以幫助開發(fā)者執(zhí)行常見的編程任務(wù),這個路徑樹可以作為參考,幫助你更好地理解 Rust 標(biāo)準(zhǔn)庫的結(jié)構(gòu)和模塊之間的關(guān)系,本文介紹 Rust 標(biāo)準(zhǔn)庫的結(jié)構(gòu),并提供相應(yīng)的 use 路徑,感興趣的朋友一起看看吧2024-05-05