SQLite Forum

Queries across databases
Login

Queries across databases

(1) By anonymous on 2021-09-21 23:05:36 [link]

I am using SQLite3.DLL with C# (Windows).

How do I achieve

> sqlite> attach database 'c:/sqlite/db/contacts.db' as contacts;

using SQL?

In other words, what should be the second parameter of sqlite3_exec to attach a database?

(2) By Larry Brasfield (larrybr) on 2021-09-21 23:50:07 in reply to 1 [link]

Have you tried that same SQL, as the C# literal "attach database 'c:/sqlite/db/contacts.db' as contacts"? And if not, why not? If the shell which is published by this project (and produces "sqlite> " as its prompt) accepts your SQL, you can be reasonably sure that SQLite3.DLL will accept it (or should if it is built with this project's source.)

Going beyond your posed question: How are you "using SQLite3.DLL with C#"? That DLL does not present an interface easily used from C#, and the substantial work necessary to use it that way creates many learning opportunities. So if you have tried the obvious 2nd argument to sqlite3_exec() without success, you will need to provide much more detail before you can reasonably hope for help here.

(3) By Ryan Smith (cuz) on 2021-09-22 00:04:35 in reply to 1

The SQL for it is simply:

```
ATTACH DATABASE 'c:/sqlite/db/contacts.db' AS contacts;

and in program terms:
sqlite3_exec(pdb, "ATTACH DATABASE 'c:/sqlite/db/contacts.db' AS contacts;" ... );
```
Which should all already work according to your given example, but note that:

- attaching has to be allowed by your version of SQLite (or C# wrapper for it),
- there are limits to how many DBs can be attached,
- you need read/write privileges to the folder of the file you are attaching,
- and to the DB file itself. (Depending on the Journal mode and intended use.)


To get rid of it again you can do:

```
DETACH DATABASE contacts;
```

(4) By anonymous on 2021-09-22 06:25:58 in reply to 3 [link]

> [How are you "using SQLite3.DLL with C#"? ](https://sqlite.org/forum/forumpost/aa286f302c4ee405?t=h)

We've been here! I am loading SQLite3.DLL dynamically in C#. Reasons:

- Available wrappers and drivers are statically linked to one or another historical version of SQLite3. 
- I want to be able to choose the version of SQLite3 that I use; I can keep SQLite3.EXE and SQLite3.DLL in sync.
- There are (were) several versions of SQLite3 on my computer even before I started.

When I started (v 3.34 I think), I could

> .read '| "D:/SQLite32/SCRIPTS/Techniques/N Random Records.BAT" tblSalesRevenue 10'


N Random Records.BAT has:

<code>echo SELECT * FROM [%1] ORDER BY RANDOM() LIMIT %2;</code>

I wanted to be able to use all the math functions without [loading extensions](https://sqlite.org/forum/forumpost/f20c913919437d42?t=h).

The SQLite3.DLL APIs aren't easy to start with; however, with perseverance, the insights gleaned from this forum and other online resources, it all falls into place quite readily.

> ATTACH DATABASE 'c:/sqlite/db/contacts.db' AS contacts;

I am certain that I tried this before posting. <i> (!! did I have SELECT as a prefix !!) </i> Thanks for the help ...

> sqlite3_exec(pdb, "ATTACH DATABASE 'c:/sqlite/db/contacts.db' AS contacts;" ... );

.. is what I need (works).

> there are limits to how many DBs can be attached,

I understand that the default number is 10 but that it would be 125: correct?