Skip to content

How to use variable in REMOVE statement #2905

Closed Answered by naisofly
plus1xp-everyday asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @plus1xp-everyday !

Unfortunately, our parser doesn't currently allow using variable bindings in some statements like DEFINE and REMOVE, as in this case. We need to put that directly in the query itself. So instead of
let sql = "REMOVE TABLE $table";

we can use
let sql = format!("REMOVE TABLE {table}");

So that function can be written as

async fn remove(db: &Surreal<Db>, table: &str) -> Result<()> {
    let sql = format!("REMOVE TABLE {table}");
    db.query(sql).await?.check()?;
    Ok(())
}

Another interesting option for Rust users only is to bypass the parser completely and pass in the remove statement directly:

async fn remove(db: &Surreal<Db>, table: &str) -> Result<()> {
    let…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@plus1xp-everyday
Comment options

Answer selected by naisofly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants