Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: How easy insert jpg image to MySQL mariadb bloob with rust
type or paste code here
`Cargo.toml`.
```toml
[dependencies]
mysql = "20.1"
r2d2 = "0.8"
r2d2-mysql = "0.15"
```
###
use mysql::*;
use mysql::prelude::*;
use std::fs::File;
use std::io::{self, Read};
fn main() -> Result<()> {
let db_url = "mysql://username:password@localhost:3306/database_name";
let pool = Pool::new(db_url)?;
let mut conn = pool.get_conn()?;
let image_path = "path/to/your/image.jpg"; //
//
let mut file = File::open(image_path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
//
conn.exec_drop(
"INSERT INTO images_table (image_data) VALUES (:data)",
params! {
"data" => buffer,
},
)?;
println!("ok succes .");
Ok(())
}
CREATE TABLE images_table (
id INT AUTO_INCREMENT PRIMARY KEY,
image_data LONGBLOB NOT NULL
);
1 post - 1 participant
🏷️ Rust_feed