[odb-users] How to run a select query to a DB from C++?
Visa Karthick
Visa.Karthick at irisi.com.au
Sun Sep 3 23:41:34 EDT 2023
I am troubleshooting a program and need some assistance. Below is the code.
void initialiseDB(std::unique_ptr<odb::database>& db) {
try
{
odb::transaction t2(db->begin());
// Find function should be invoked here
auto tankType = std::make_shared<EntityType>("Tank");
db->persist(tankType.get()); // This inserts a new record with value Tank in the EntityType table.
t2.commit();
}
catch (const odb::exception &e)
{
std::cerr << e.what() << std::endl;
return;
}
}
The above code works fine for inserting records in to a Sqlite table. The problem is it keeps adding the same row again and again When I run the program. I need to insert records only when the table is empty. There is another function for running a select query in the database. It is invoked by the below code.
template <typename T>
inline typename object_traits<T>::pointer_type database::
find (const typename object_traits<T>::id_type& id)
{
return find_<T, id_common> (id);
}
template <typename T>
inline bool database::
find (const typename object_traits<T>::id_type& id, T& obj)
{
return find_<T, id_common> (id, obj);
}
I need to invoke the find function first, and only if it doesnt return any result persist function should be called. I am unsure what parameters to be passed from my current scope. Could you please advise?
I am not sure what parameters to be passed.
Visa Karthick | Electrical Engineer | Victoria
IRIS Integration Pty Ltd
T:+61 1300 240 036 | M : +61 405918505 | visa.karthick at IRISI.com.au<mailto:visa.karthick at IRISI.com.au>
More information about the odb-users
mailing list