rust + dockermysql简单查询使用

依赖toml

[package]
name = "carm"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mysql = "23.0.1"
chrono = "0.4"

main.rs

use mysql::*;
use mysql::prelude::*;
fn main(){
    //设置连接字符串
    let url="mysql://root:123456@localhost:3306/mysql";
    //创建连接池
    let pool=Pool::new(url).unwrap();
    //连接数据库
    let mut conn=pool.get_conn().unwrap();
    //数据库操作
    //1.查询user表
    //方式1:流式查询  数据逐行读取,数据不会存储在内存中
    conn.query_iter("Select * from test").unwrap()
        .for_each(|row|{

            println!("ok");
        });
    // //方式2:将数据集取出存储在Vec中
    let res:Vec<(String)>=conn.query("Select name from test").unwrap();
    for r in res{
        println!("name:{}",r);
    }
    
}

mysql数据


test表
name字段   | test | test | test

你可能感兴趣的:(为c++使用cmake,rust,开发语言,后端)