rust连接postgresql数据库

同步操作

引入crate:

复制代码
postgres = "0.19.7"
rust 复制代码
use postgres::{Client, NoTls, error::Error};

fn main() -> Result<(), Error> {
    let mut client = Client::connect("host=localhost port=5432 dbname=xxxxxdb user=postgres password=123456", NoTls).unwrap();
    for row in client.query("select id,name,file_name,create_time::TEXT,valid from xxxxx", &[])?{
        let id: i32 = row.get("id");
        let name: &str = row.get("name");
        let file_name: &str = row.get("file_name");
        let create_time: &str = row.get(3);
        let valid: bool = row.get(4);
        println!("id:{}, name:{}, file_name:{},create_time:{:?} valid:{}", id, name, file_name,create_time, valid);
    }

     let rows = client.query("select count(*)::integer as a from xxxxx where id=$1", &[&1])?;
     let cnt: i32 = rows[0].get("a");
     println!("cnt={}", cnt);

    Ok(())
}

注意:用户名不要使用root。否则报错:

thread 'main' panicked at src\main.rs:5:115:

called `Result::unwrap()` on an `Err` value: Error { kind: Parse, cause: Some(Custom { kind: InvalidInput, error: Utf8Error { valid_up_to: 0, error_len: Some(1) } }) }

注意:select count(*) ::integer as a from xxxx的写法,做postgres内的类型转换,否则count(*)默认作为i8返回,rust获取数据时报错。

异步操作

引入Crate:

复制代码
tokio = {version = "1.35.1", features = ["full"]}
tokio-postgres = "0.7.10"
chrono = "0.4.34"
rust 复制代码
use tokio_postgres::{NoTls, Error};
use chrono::prelude::{DateTime, Utc};
use std::time::SystemTime;

#[tokio::main]
async fn main() -> Result<(), Error> {
    println!("测试tokio异步版本postgres操作");
    let (client, connection) = tokio_postgres::connect("host=localhost port=5432 dbname=xxxxxdb user=postgres password=123456", NoTls).await.unwrap();
    //启动线程监控数据库连接是否正常
    tokio::spawn(async move {
        if let Err(e) = connection.await{
            eprintln!("connection error:{}", e);
        }
    });
    let rows = client.query("select id,name,file_name,create_time,valid from xxxxx", &[]).await?;
    for row in rows{
        let id: i32 = row.get("id");
        let name: &str = row.get("name");
        let file_name: String = row.get("file_name");
        let create_time: SystemTime = row.get("create_time");
        let dt: DateTime<Utc> = create_time.clone().into();
        println!("id={}, name={}, file_name={}, create_time={}", id, name, file_name, dt.format("%Y-%M-%d %H:%m:%S"));
    }

    let rows = client.query("select max(id)::integer from xxxxx", &[]).await?;
    let max_id:i32 = rows[0].get(0);
    println!("max id={}", max_id);
    let new_id = max_id + 1;
    let new_name: String = format!("xxxxx-{}", new_id);
    let new_file_name: String = format!("xxxxx_file_name-{}", new_id);
    let modify_row_cnt = client.execute("insert into xxxxx(id,name,file_name,create_time,valid) values($1,$2,$3,$4,$5)", &[&new_id, &new_name, &new_file_name, &SystemTime::now(), &true]).await?;
    println!("affect row count:{}", modify_row_cnt);

    let affect_row_count = client.execute("update xxxxx set file_name='xxxxx_file_name-' || id::TEXT where file_name=''", &[]).await?;
    println!("affect row count:{}", affect_row_count);

    let affect_row_count = client.execute("delete from xxxxx where id=$1", &[&new_id]).await?;
    println!("delete row count:{}", affect_row_count);

    let trans = client.transaction().await?;
    trans.execute("update xxxxx set valid=$1,remark=name || id::TEXT where id=$2", &[&false, &1]).await?;
    trans.execute("update xxxxx set valid=$1,remark=name || id::TEXT where id=$2", &[&true, &2]).await?;
    trans.commit().await?;
    Ok(())
}

注意:postgresql中的timestamp类型可以直接转换为rust中的SystemTime;要转换为我们熟悉的年月日时分秒格式,还需要借助chrono crate。

代码中含增、删、改、查、事务范例。

相关推荐
阳冬园8 分钟前
mysql数据库 主从同步
数据库·主从同步
Amd79425 分钟前
PostgreSQL 的特点
postgresql·数据类型·并发控制·关系型数据库·安全性·可扩展性·数据库特性
Mr.131 小时前
数据库的三范式是什么?
数据库
Cachel wood1 小时前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Python之栈2 小时前
【无标题】
数据库·python·mysql
风_流沙2 小时前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch
亽仒凣凣2 小时前
Windows安装Redis图文教程
数据库·windows·redis
亦世凡华、2 小时前
MySQL--》如何在MySQL中打造高效优化索引
数据库·经验分享·mysql·索引·性能分析
m0_748236112 小时前
Calcite Web 项目常见问题解决方案
开发语言·前端·rust
YashanDB2 小时前
【YashanDB知识库】Mybatis-Plus调用YashanDB怎么设置分页
数据库·yashandb·崖山数据库