Skip to content

Instantly share code, notes, and snippets.

@DanielHe4rt
Last active April 19, 2023 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielHe4rt/523e277dca4051d9e53e62bda327cdfb to your computer and use it in GitHub Desktop.
Save DanielHe4rt/523e277dca4051d9e53e62bda327cdfb to your computer and use it in GitHub Desktop.
Why this doesn't work plz I just want a struct to work in my example
// on the second example i know that i need to use ValueList (from scylla) but it stills crashes on Uuid problem.
use anyhow::Result;
use chrono::{NaiveDate, Utc};
use scylla::{FromRow, Session, SessionBuilder, ValueList};
use serde::{Deserialize, Serialize};
use std::time::Duration as TimeoutDuration;
use uuid::Uuid;
#[derive(Debug)]
struct Song {
id: Uuid,
title: Option<String>,
album: Option<String>,
artist: Option<String>,
created_at: NaiveDate,
updated_at: NaiveDate,
}
#[tokio::main]
async fn main() -> Result<()> {
let session: Session = SessionBuilder::new()
.known_nodes(&["node-0.aws_sa_east_1.432bce89e0ba3222d3b2.clusters.scylla.cloud:9042"])
.connection_timeout(TimeoutDuration::from_secs(30))
.user("scylla", "***")
.build()
.await
.expect("connection refused");
let song = Song {
id: Uuid::new_v4(),
title: Some(String::from("Stairway to Heaven")),
album: Some(String::from("Led Zeppelin IV")),
artist: Some(String::from("Led Zeppelin")),
created_at: Utc::now().date_naive(),
updated_at: Utc::now().date_naive(),
};
session.query(
"INSERT INTO media_player.songs (id, title, album, artist, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
(song.id, song.title, song.album, song.artist, song.created_at, song.updated_at),
).await?;
// or
session.query("INSERT INTO media_player.songs (id, title, album, artist, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",(song)).await?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment