sqlite3: base de datos principal de solo lectura y ATTACH

I am looking to understand the implications of using ATTACH with databases with different read-write permissions.

I have a scenario where I need to access a large database (approx. 512MB) that resides in a read-only filesystem. There is also a small read-write database with the same schema that resides in a read-write filesystem. The read-only database provides the base data used in my scenario, with infrequent data updates stored in the read-write database.

Currently I open these two databases in separate connections and the code that maintains the connections is responsible for presenting a unified view of the data to its clients. For example, this means that the code has to merge query results from the read-only and read-write databases, etc. I realize that this setup is inelegant (and likely suboptimal) and have been looking to use the ATTACH command to create a unified view of the data in SQL rather than C++.

I am wondering then whether there are any particular gotchas related to attaching read-only and read-write databases that I should be aware of. I am looking at one of the following ATTACH scenarios:

  1. Open the read-only database as main and ATTACH the read-write database. This is my preferred solution.
  2. Open the read-write database as main and ATTACH the read-only database.
  3. A third alternative?

A few google queries pointed to messages suggesting problems in scenario (1). Because I did not find a definitive answer and because my own testing using sqlite 3.6.13 did not reveal any problems, I am posting this question.

Thank you for any insights.

preguntado el 09 de enero de 11 a las 05:01

1 Respuestas

La documentación doesn't seem to mention any caveats with attaching read-write databases to read-only ones.

So my assumption is that what you should expect to happen were the databases to be opened separately should also happen when you open one and attach the other.

I put your scenario 1 to the test and it seems to work fine. Here is what I tried:

[someone@somewhere tmp]$ echo .dump | sqlite3 big_readonly_db
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE foo (a INT);
INSERT INTO "foo" VALUES(1);
INSERT INTO "foo" VALUES(2);
INSERT INTO "foo" VALUES(3);
INSERT INTO "foo" VALUES(4);
INSERT INTO "foo" VALUES(5);
COMMIT;
[someone@somewhere tmp]$ echo .dump | sqlite3 small_readwrite_db
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE bar (a INT);
COMMIT;
[someone@somewhere tmp]$ chmod -w big_readonly_db
[someone@somewhere tmp]$ ls -l big_readonly_db
-r--r--r-- 1 someone someone 2048 Apr 12 21:41 big_readonly_db
[someone@somewhere tmp]$ sqlite3 big_readonly_db
SQLite version 3.7.7.1 2011-06-28 17:39:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> attach database small_readwrite_db as rw;
sqlite> insert into bar select * from foo;
sqlite> select * from bar;
1
2
3
4
5

Respondido 12 Abr '12, 23:04

This doesn't work if the main database is opened with SQLITE_OPEN_READONLY bandera. - Yakov Galka

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.