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。

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

相关推荐
兩尛1 小时前
订单状态定时处理、来单提醒和客户催单(day10)
java·前端·数据库
web2u1 小时前
MySQL 中如何进行 SQL 调优?
java·数据库·后端·sql·mysql·缓存
Elastic 中国社区官方博客2 小时前
使用 Elasticsearch 导航检索增强生成图表
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
小金的学习笔记2 小时前
RedisTemplate和Redisson的使用和区别
数据库·redis·缓存
新知图书3 小时前
MySQL用户授权、收回权限与查看权限
数据库·mysql·安全
文城5213 小时前
Mysql存储过程(学习自用)
数据库·学习·mysql
沉默的煎蛋3 小时前
MyBatis 注解开发详解
java·数据库·mysql·算法·mybatis
呼啦啦啦啦啦啦啦啦3 小时前
【Redis】事务
数据库·redis·缓存
HaoHao_0103 小时前
AWS Serverless Application Repository
服务器·数据库·云计算·aws·云服务器
C语言扫地僧3 小时前
MySQL 事务及MVCC机制详解
数据库·mysql