giucas
March 18, 2020, 2:26pm
1
Hello, I want to handle a QueryReturnedNoRows error in rusqlite crate.
My code is:
let preference_iter = conn.query_row(
"SELECT * FROM preferences where key='server'",
params![],
|row| {
Ok(Preference {
id: row.get(0)?,
key: row.get(1)?,
value: row.get(2)?,
})
}).expect("select failed");
In case of 0 result thread 'main' panicked at 'select failed: QueryReturnedNoRows'
How can I solve this problem?
Thanks
Gianluca
alice
March 18, 2020, 2:32pm
2
Please use code blocks for code and errors:
```
// your code here
```
As for your question, you can inspect the error instead of using expect
.
match conn.query_row(...) {
Ok(res) => {
// got a row
}
Err(rusqlite::Error::QueryReturnedNoRows) => {
// no rows
}
Err(err) => {
// something else went wrong
panic!("Select failed: {}", err);
}
}
giucas
March 20, 2020, 10:47am
3
Thanks for your reply Alice but I am a beginner in Rust. I write this:
match conn.query_row("SELECT * FROM preferences where key='server'", params![],|row| {
Ok(Preference {
id: row.get(0)?,
key: row.get(1)?,
value: row.get(2)?,
})}){
Ok(server_preference) => Ok(Some(server_preference)),
Err(Error::QueryReturnedNoRows) => Ok(None),
Err(err) => Err(err),
};
but my variable server_preference is not valorise.
Thanks again
alice
March 20, 2020, 3:48pm
4
Please edit your post to use code blocks for code and errors:
```
// your code here
```
alice
March 20, 2020, 3:53pm
6
So why doesn't it work? I don't know what valorise means.
giucas
March 20, 2020, 3:55pm
7
I edited my code like this:
let server_preference = match conn.query_row("SELECT * FROM preferences where key='server'", params![],|row| {
Ok(Preference {
id: row.get(0)?,
key: row.get(1)?,
value: row.get(2)?,
})}){
Ok(x) => Ok(Some(x)),
Err(Error::QueryReturnedNoRows) => Ok(None),
Err(err) => Err(err),
};
How can I retrieve only Preference.value?
Thansk
alice
March 20, 2020, 3:58pm
8
You only want the value field? One option would be this:
let query = conn.query_row("SELECT * FROM preferences where key='server'", params![], |row| {
Ok(Preference {
id: row.get(0)?,
key: row.get(1)?,
value: row.get(2)?,
})
});
let server_preference = match query {
Ok(x) => Ok(Some(x.value)), // <- take out only the value
Err(Error::QueryReturnedNoRows) => Ok(None),
Err(err) => Err(err),
};
giucas
March 20, 2020, 4:02pm
9
ok it works but I retrieve Ok(Some(xxxxxx)).
How can I remove Ok(Some ?
alice
March 20, 2020, 4:05pm
10
You can match or use an if:
match server_preference {
Ok(Some(x)) => {
// use x
}
Ok(None) => {
// handle none case
}
Err(err) => {
// handle error
}
}
If you are inside a function that returns a result, you can use the question mark to propogate the error if it happens:
match server_preference? { // question mark returns on error
Some(x) => {
// use x
}
None => {
// handle none case
}
}
You can also use if instead of match:
if let Some(x) = server_preference {
} else {
}
Finally you can use unwrap
to remove errors instead of question mark.
giucas
March 20, 2020, 4:12pm
11
My code is inside a function that return a string
My function now end with:
Ok(format!("{:?}", server_preference))
If I put it into match.... I receive error value borrowed here after move
Where should I put it?
system
Closed
June 18, 2020, 4:12pm
12
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.