I have a weird error with rustqlite
. I have a database implementation within a struct that has the following method:
pub fn get_paste_by_url(&self, url: &String) -> Option<Paste> {
let mut query = self
.connection
.prepare("select * from pastes where url=?1")
.unwrap();
let paste = query.query_row([url], |p| {
Ok(Paste {
id: p.get(0)?,
url: p.get(1)?,
password_hash: p.get(2)?,
content: p.get(3)?,
date_published: p.get(4)?,
date_edited: p.get(5)?,
})
});
paste.ok()
}
The pastes
table works, and is defined so on struct Database
:
pub fn init() -> Self {
let c = Connection::open("main.db").unwrap();
match c.execute(
"create table if not exists pastes (
id integer primary key,
url text,
password text,
content text,
date_published text,
date_edited text
)",
(),
) {
Ok(_) => println!("Successfully connected to table."),
Err(e) => panic!("Database creation failed with error message: {e}"),
}
Self { connection: c }
}
I already have a record added to this table, which I can query with select * from pastes where url="hello";
from the sqlite prompt on the terminal, returning 1|hello|9b8769a4a742959a2d0298c36fb70623f2dfacda8436237df08d8dfd5b37374c|cool shit|1718103378|1718103378
. Despite this, the following test fails:
#[test]
fn does_db_work() {
assert_eq!("cool shit".to_string(), database::Database::init().get_paste_by_url(&"hello".to_string()).unwrap().content)
}
running 1 test
test tests::does_db_work ... FAILED
failures:
---- tests::does_db_work stdout ----
Successfully connected to table.
thread 'tests::does_db_work' panicked at srcmain.rs:28:99:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::does_db_work
srcmain.rs:28:99
is the unwrap
in the test.
I’m genuinely at a loss, I’ve been trying to solve this for hours but no avail. Can anyone help me?
I tried reading through the rustqlite documentation but that did not clarify anything.