Is it possible to attach a named SQLite3 :memory: database using the C API? There is of course sqlite3_open, but how do I set the name to be used in queries? Also, I could attach it by executing the ATTACH DATABASE statement, but then how do I get hold of its DB pointer?
Background:
I am using virtual tables to drive a SQLite interface to a set of external files. I am using SQLite mainly to get access to decent SQL querying and existing drivers for e.g. Python. I have defined a function open_myfiles which currently creates all the virtual tables based on the files in a given directory. The application just has to do SELECT open_myfiles('/path/to/tables') and the database is populated with the correct virtual tables.
However, I am attempting an improvement. Currently, the virtual tables are persisted to the current database and it is difficult to load multiple directories. I think that a better approach would be to attach each directory as a separate, named, :memory: database.
After some research, I found out that there is no clear connection between a C api database ptr and an attached database. I had to modify my initial approach, and here is how I solved it in case anyone else stumbles upon this.
Instead of registering the virtual table module on extension load, I register a virtual table module for each invocation of the
open_myfilesfunction. In addition to registering the module, this function also executesATTACH DATABASE ':memory:' AS 'mymod'to put the virtual tables in their respective namespace (CREATE VIRTUAL TABLEworks with qualified names as well, it turns out).All in all, it works very well. I hope this helps someone else!