Tokio_postgres and adding data problem

here is the code:

/ ### ADD borg DATA TO DATABASE ### //
#[handler]
 pub async fn borg_postlogs(res: &mut Response, req: &mut Request) {
    let (client, connection) =
        tokio_postgres::connect("host=127.0.0.1 user=username password=password dbname=database", NoTls).await.unwrap();
    tokio::spawn(async move {
        if let Err(e) = connection.await {
            eprintln!("connection error: {}", e);
        }
    });

    // POST incoming data to the database
    #[derive(Serialize, Deserialize, Extractible, Debug)]
    struct Data {
    token: String,
    date: String,
    name: String,
    hostname: String,
    duration: String,
    files: String,
    compressed_size: String,
    original_size: String,
    repository_id: String,
    backupdir: String,
    status: String,
   }

    let d = req.parse_json::<Data>().await.unwrap();
    
    client.query(
        "INSERT INTO borglogs (token, date, name, hostname, duration, files, compressed_size, original_size, repository_id, backupdir, status)
         VALUES ('$1', '$2','$3', '$4','$5', '$6','$7', '$8','$9', '$10','$11');",
        &[&d.token, &d.date, &d.name, &d.hostname, &d.duration, &d.files, &d.compressed_size, &d.original_size, &d.repository_id, &d.backupdir, &d.status])
    .await.unwrap();

    
    let e = format!("Data for token {} added to database", d.token);
    res.render(Text::Html(e));
}

The error i get in return is:

thread 'tokio-runtime-worker' panicked at 'expected 0 parameters but got 11', /config/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-postgres-0.7.7/src/query.rs:162:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Which, to me seems odd because i 11 datapoints for 11 columns? I'm missing something, but i can't seem to figure out what.

Have you tried removing the single quotes around $n? I couldn't find much on the interpolation syntax of tokio-postgres, but I find it probable that '$n' is escaped to mean literally $n, not the interpolated value.

1 Like

THAT was indeed the solution :slight_smile: thank you very much!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.