Howto to access the tables of an attached database?

Nov 29, 2013 at 4:29 PM
Hi,

There are two identical database (same tables, different content) A and B. B is attached to A:
database dbA = new  Database(dbAFile);
await dbA.OpenAsync();

await dbA.ExecuteStatementAsync("ATTACH B.sqlite AS AttachDB");

query = "SELECT * FROM SomeTable";
using (Statement sqlStatement = await dbA.PrepareStatementAsync(query)) {
   while (await sqlStatement.StepAsync()) {
      ...List Content of SomeTable in Table A --> No Problem
   }
}

query = "SELECT * FROM AttachDB.SomeTable";
using (Statement sqlStatement = await dbA.PrepareStatementAsync(query)) {
   while (await sqlStatement.StepAsync()) {
      ...List Content of SomeTable in Table B --> Crash in PrepareStatementAsync
   }
}
When using to access the Table "SomeTable" in the attached database an Exception is thrown when preparing the statment. The Exception does not show the source of the problem but I managed to get the SQL error message, which is:

"no such table: AttachDB.SomeTable"

Of course database B has a Table "SomeTable". When I connect to this database directly I can access table "SomeTable" without any problem.

I thought that I could be a problem that B has exactly the same tables as A and create a table "OnlyInB" in B. But accessing AttachDB.OnlyInB also fails. According to the sqlite doc one can skip the database name when the table name is unique. Thus "OnlyInB" should work as well. But is does not. "SELECT * FROM OnlyInB" shows "no such table: OnlyInB"

I testes the same scenation with the "SQLite Manager" Extension of Firefox (opened A, attached, B...) Selecting Data from AttachDB.SomeTable or any other table in B is not problem there. The Problem only occurs when using sqlite-winrt.

I also tested if there could be any problem with attaching B to A. But there isn't. When I try to attach B twice or to attach a third database using "AttachDB" again an error is shown that clearly states that B is correctly attached to A. The attaching seems to work perfect, except the problem that I cannot access any tables in B...

Any advise?
Thank you very much!
Nov 29, 2013 at 6:38 PM
Hi,

I solved the issue. Problem was the way I attached B:
await dbA.ExecuteStatementAsync("ATTACH B.sqlite AS AttachDB");
This did not throw any exception and in fact it returned the SQLiteStatus Code "Done". So I assumed that the it worked. Well it worked, at least something was attached, but not B.sqlite.

One cannot only use the filename here. The complete to the file has to be used in order to let this work... After attaching B correctly everything works as expected.