Skip to content

Instantly share code, notes, and snippets.

@TheLexoPlexx
Created January 29, 2024 14:35
Show Gist options
  • Save TheLexoPlexx/01a050cdef973f72c2673fd537837050 to your computer and use it in GitHub Desktop.
Save TheLexoPlexx/01a050cdef973f72c2673fd537837050 to your computer and use it in GitHub Desktop.
tiberius encoding error
use dotenv::dotenv;
use tiberius::{AuthMethod, Client, Config, Query};
use tokio::net::TcpStream;
use tokio_util::compat::TokioAsyncWriteCompatExt;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv().ok();
let db_host = std::env::var("DATABASE_HOST").expect("DATABASE_URL must be set");
let db_user = std::env::var("DATABASE_USER").expect("DATABASE_USER must be set");
let db_pass = std::env::var("DATABASE_PASS").expect("DATABASE_PASS must be set");
let db_name = std::env::var("DATABASE_NAME").expect("DATABASE_NAME must be set");
let mut config = Config::new();
config.host(db_host);
config.authentication(AuthMethod::sql_server(db_user, db_pass));
config.database(db_name);
config.trust_cert(); //VERY BAD, REMOVE FOR PROD
let tcp = TcpStream::connect(config.get_addr()).await?;
tcp.set_nodelay(true)?;
let mut client = match Client::connect(config, tcp.compat_write()).await {
Ok(client) => client,
Err(e) => {
println!("Error connecting to server: {}", e);
return Ok(());
}
};
let select = Query::new("<select-here>");
let mut query = match select.query(&mut client).await {
Ok(res) => res,
Err(e) => {
println!("Error querying server: {}", e);
return Ok(());
}
};
let result = match query.into_results().await {
Ok(rest) => {
dbg!(rest);
}
Err(e) => {
println!("Error getting row: {}", e);
return Ok(());
}
};
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment