I used rust's mysql library to connect to the mysql database, and the result of the query was that every data item was a character type
fn get_row<T: DeserializeOwned + Default>(r: Row) -> T {
let mut json_obj = json!({});
for col in r.columns_ref() {
let col_key = col.name_str().to_string();
let col_val = if let Value::Bytes(v) = &r[col.name_str().as_ref()] {
std::str::from_utf8(v).unwrap()
} else {
""
};
println!("------{}------{:#?}-----", col_key, col.column_type());
json_obj[col_key] = col_val.into();
}
let res_str = serde_json::to_string_pretty(&json_obj).unwrap();
let res: T = serde_json::from_str(&res_str).unwrap();
res
}
fn info<T>(conn: PooledConn) -> Result<T, std::io::Error>
where
T: DeserializeOwned + Default,
{
let sql = format!("select * from company");
let result: Option<Row> = conn.query_first(sql).expect("Data query error");
if let Some(row) = result {
let res: T = get_row(row);
return Ok(res);
}
Ok(T::default())
}
// This is the structure passed in by calling the info method
#[derive(Deserialize, Default, Debug)]
struct Company {
id: String,
name: String,
status: u32,
err_info: String,
account_id: String,
create_at: String,
update_at: String,
}
The data obtained by calling the info method is as follows, as if each data is a string, through the printing type, it should be the corresponding data of different types, how to restore the original data, ask for help
Company {
id: "2017612633062042215",
name: "Yangyang",
status: "100",
err_info: "",
account_id: "0",
create_at: "1710993521",
update_at: "1711000831",
}
------id------MYSQL_TYPE_LONGLONG-----
------name------MYSQL_TYPE_VAR_STRING-----
------status------MYSQL_TYPE_SHORT-----
------err_info------MYSQL_TYPE_VAR_STRING-----
------account_id------MYSQL_TYPE_LONGLONG-----
------create_at------MYSQL_TYPE_LONGLONG-----
------update_at------MYSQL_TYPE_LONGLONG-----
The following is the data table structure