描述
-
如果使用默认的连接方式此扩展并不会对连接地址中的
#
等符号进行转义:
let url = encode("mysql://root:abc#1234@127.0.0.1:3306/test");
方法:一
- 直接按照转义表对具体字符进行替换:如
#
替换为%23
let url = encode("mysql://root:abc%231234@127.0.0.1:3306/test");
方法:二
-
使用
urlencoding
扩展的encode
方法对其进行转义然后再拼接起来
let url = format!("{}{}{}", "mysql://账号:", encode("密码"), "@地址:端口/库名");
示例
use mysql::*;
use mysql::prelude::*;
use urlencoding::encode;
#[derive(Debug, PartialEq, Eq)]
struct Payment {
customer_id: i32,
amount: i32,
account_name: Option<String>,
}
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let url = format!("{}{}{}", "mysql://账号:", encode("密码"), "@地址:端口/库名");
let mut conn = Pool::new(&*url)?.get_conn()?;
let selected_payments = conn
.query_map(
"SELECT ID as customer_id, type as amount, config_name as account_name from qbweb_export_config",
|(customer_id, amount, account_name)| {
Payment { customer_id, amount, account_name }
},
)?;
dbg!(selected_payments);
Ok(())
}
标签:name,url,MySQL,地址,let,Rust,mysql,encode,特殊字符
From: https://www.cnblogs.com/ruster/p/16812786.html