[odb-users] A view on top of several tables from different sqlite files (DB)

Weiss, Yoav yoav.weiss at intel.com
Sun Sep 6 11:10:29 EDT 2015


Sounds promising. I still have an issue though.

When I call m_p_db->execute ("ATTACH DATABASE cell.sqlite AS cells") I get:
operation can only be performed in transaction

So I did: m_p_db->connection()->execute ("ATTACH DATABASE cell.sqlite AS cells")
Which worked fine, but the first query (using m_p_db->query_one()) on a table inside cells DB, I got:
1: no such table: hier_version

I think this is caused by me using the connection pointer, but I'm not sure.

Table names are unique across databases

Yoav


-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com] 
Sent: Wednesday, September 02, 2015 18:32
To: Weiss, Yoav
Cc: odb-users at codesynthesis.com
Subject: Re: [odb-users] A view on top of several tables from different sqlite files (DB)

Hi Yoav,

Weiss, Yoav <yoav.weiss at intel.com> writes:

> #pragma db view object (Cell = c) \
>                 object (Instance = i : c::m_id == i::m_cell_id) \
>                 query((?) + " GROUP BY " + c::m_name) struct 
> CellOccCount {
>     #pragma db column("c.cell_name")
>     std::string m_name;
> 
>     #pragma db column ("count(*)")
>     long m_count;
> };
> 
> And the way I query the view is:
> odb::result<CellOccCount> 
> results(m_p_db->query<CellOccCount>(query_cond));
> 
> Now suppose these 2 tables (Cell and Instance) are located in 2 
> different sqlite databases. My questions are:
> 
> 1. How should I modify the view (if at all) to reflect this?

The view doesn't need to be modified, but see #2 below.


> 2. On which odb::database should I initiate the query (assuming I have
>    2 instances - one for each database)?

No, that's not going to work. You cannot have a query span multiple SQLite connections. Instead, what you will have to do is attach the second database file to the existing connection with the SQLite ATTACH DATABASE statement:

https://www.sqlite.org/lang_attach.html

In ODB you can use execute(), for example:

m_p_db->execute ("ATTACH DATABASE cell.sqlite AS cells");

If you don't have identically names tables in the two databases, then you should be all set. Otherwise, you would have to qualify the table names, for example:

#pragma db object table("cells.Cell")
class Cell
{
  ...
};

Boris
---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.




More information about the odb-users mailing list