[odb-users] "Access violation reading location" when querying a
view
Boris Kolpackov
boris at codesynthesis.com
Tue May 20 07:23:24 EDT 2025
Seiler Matthias SIGCH <Matthias.seiler at stadlerrail.com> writes:
> ```
> transaction t(db->begin());
> auto q = "Switch.Id='" + switchId + "'";
> auto viewRes = *db->query_one<TpEntityIdBySwitch>(q);
> t.commit();
> ```
>
> Thus, given a `Switch.Id` (whose table has a foreign key to `TpEntity`
> on `Switch.TpEntity`), I want to get the value of `TpEntity.Id`. It
> works fine if the passed ID exists. However, if it does not, I get
> an "Access violation reading location ...", which I'm not able to
> catch. How can I go around this issue such that it does not crash,
> maybe getting "some" result (e.g., NULL or optional) regardless of
> if the table contains the ID?
query_one() return a pointer for exactly this reason (which you
then unconditionally derefence). For details see:
https://www.codesynthesis.com/products/odb/doc/manual.xhtml#4.3
More information about the odb-users
mailing list