Open
Description
Question
I understand that in order to get data from a table I need to define a struct with the data I expect, and for specific queries I can specify specific structs with the specific data I am querying, but what if I may or may not query data. How do I query to the db where one time I might want the fields id, name, and age, and the next time I just want id and name?
Example
Currently, if I use this code (and user_input
is false), I get an runtime error that tells me that age needs to be included in the select query. I want to be able to sometimes query for, in this case, the age
column, but without having to define another struct, leave out the age
column in the next query. Is this possible?
let user_input: bool = /* ... */
let mut age = String::from("");
if user_input {
age = " age"
}
// How to make age possibly queried?
// Option doesn't work as it just makes
// age act as nullable in the mysql table definition
#[derive(sqlx::FromRow)]
struct Person { id: i64, name: String, age: Option<u8> }
// Sometimes we query fields id, name, age and other times just id and name
let query = format!("SELECT id, name,{} FROM person", age);
let mut rows = sqlx::query_as::<_, Person>(query)
.fetch(&mut conn);
Metadata
Metadata
Assignees
Labels
No labels