Skip to navigation
Rust actix web example to call a mssql server
01.03.21
Cargo.toml: [package] name = "sapir" version = "0.1.0" authors = ["veto"] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] actix-web = "3" typename = "0.1.1" serde = { version = "1.0", features = ["derive"] } tiberius = {version ="0.5.9"} anyhow = {version='1.0.34'} async-std = {version = "1.9.0", features = ["attributes"]} futures = {versions="0.3.12"} src/main.rs: use actix_web::{get, web, App, HttpServer, Responder}; use serde::{Serialize, Deserialize}; use tiberius::{Client, Config, AuthMethod}; use async_std::net::TcpStream; #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() .service(test) }) .bind("127.0.0.1:8089")? .run() .await } #[get("/")] async fn test() -> impl Responder { let r = mssql().await.unwrap(); //println!("{:?}", r); return web::Json(r) } rust actix web example to call a mssql server and return an API json: async fn mssql() ->anyhow::Result
> { let mut _config = Config::new(); _config.host("8.8.8.8"); _config.port(1433); _config.authentication(AuthMethod::sql_server("USERNAME", "PASSWORD")); _config.trust_cert(); let _tcp = TcpStream::connect(_config.get_addr()).await?; _tcp.set_nodelay(true)?; let mut client = Client::connect(_config, _tcp).await?; let stream = client.query("SELECT top 5 s.yz3_qty AS qty,s.yz3_style AS style FROM SL.dbo.yz3_stock AS s WHERE s.yz3_location = 1 AND s.yz3_style NOT IN ('','**')", &[&"B%"]).await.unwrap(); let mut _vec = stream.into_results().await?; let mut stock:Vec
= Vec::new(); for i in _vec.into_iter() { for row in i.into_iter() { let _cols = row.columns(); let _qty: Option
= row.get("qty"); let _style: Option<&str> = row.get("style"); stock.push(Stock{style:_style.unwrap().to_string(), qty:_qty.unwrap()}); } } Ok(stock) } // Helper Functions //fn print_type_of
(_: &T) { // println!("{}", std::any::type_name::
()) //} #[derive(Serialize, Deserialize, Debug)] struct Stock { style: String, qty: i32, } source: * https://docs.rs/tiberius/0.5.9/tiberius/index.html * https://actix.rs/
https://actix.rs/
Reply
Anonymous
Information Epoch 1732558907
Portable data is as important as portable code.
Home
Notebook
Contact us