0

Does anybody know how to fix this? Trying to make a REST API with Rust(warp crate for API and ODBC-API Crate to connect to MS SQL Server ).

code:

use odbc_api::{ConnectionOptions, Environment, Cursor};
use serde::Serialize;
use warp::{Filter, Rejection, Reply};

#[derive(Serialize)]
struct Data {
    name: String,
    price: f64,
    quantity: i32,
}

async fn fetch_data() -> Result<Vec<Data>, odbc_api::Error> {
    let env = Environment::new()?;
    let connection_string = "Driver={ODBC Driver 17 for SQL Server};Server=Connection_String ..."; // Connection details
    let mut conn = env.connect_with_connection_string(connection_string, ConnectionOptions::default())?;
    let sql = "SELECT name, price, quantity 
    FROM dbo.tblGrocery
    WHERE status = 1
    ORDER BY name ASC"; // SQL query
    let cursor = conn.execute(sql, ())?;
    let mut data = Vec::new();
    if let Some(cursor) = cursor {
        while let Some(row) = cursor.next_row()? {
            let name: String = row.get_col(1)?;
            let price: f64 = row.get_col(2)?;
            let quantity: i32 = row.get_col(3)?;
            data.push(Data { name, price, quantity });
        }
    }
    Ok(data)
}

async fn handle_request() -> Result<impl Reply, Rejection> {
    let data = fetch_data().await.unwrap();
    Ok(warp::reply::json(&data))
}

#[tokio::main]
async fn main() {
    let route = warp::get().and(warp::path("data")).and_then(handle_request);
    warp::serve(route).run(([127, 0, 0, 1], 3030)).await;
}

Getting this error

error[E0599]: no method named `get_col` found for struct `CursorRow` in the current scope
  --> src\main.rs:24:36
   |
24 |             let name: String = row.get_col(1)?;
   |                                    ^^^^^^^ method not found in `CursorRow<'_>`      

error[E0599]: no method named `get_col` found for struct `CursorRow` in the current scope
  --> src\main.rs:25:34
   |
25 |             let price: f64 = row.get_col(2)?;
   |                                  ^^^^^^^ method not found in `CursorRow<'_>`

error[E0599]: no method named `get_col` found for struct `CursorRow` in the current scope
  --> src\main.rs:26:37
   |
26 |             let quantity: i32 = row.get_col(3)?;
   |                                     ^^^^^^^ method not found in `CursorRow<'_>`

my Cargo.toml:

[package]
name = "neat-api"
version = "0.1.0"
edition = "2021"

[dependencies]
odbc-api = "0.57.0"
warp = "0.2"
serde = { version = "1.0", features = ["derive"] }
Jmb
  • 18,893
  • 2
  • 28
  • 55
  • Try with `let mut price: f64 = 0.0; row.get_data (2, &mut price)?;` – Jmb Jun 12 '23 at 07:08
  • Also note that database text is represented by a `CString` in Rust, not a regular `String`, so you would need `let mut name = CString::new (""); row.get_data (1, &mut name)?;` – Jmb Jun 12 '23 at 07:11

0 Answers0