SQLite Forum

Using PRAGMA functions with attached DBs could use an example or two
Login

Using PRAGMA functions with attached DBs could use an example or two

(1) By ddevienne on 2023-01-04 15:57:11 [link] [source]

Hi. Just a bit of feedback on the doc for pragmas.

For traditional pragmas, both the railroad diagram and sentence below from the doc are clear enough:

A pragma may have an optional (schema-name). before the pragma name

But for PRAGMA functions OTOH, it is less clear.

The PRAGMA argument and schema, if any, are passed as arguments to the table-valued function, with the schema as an optional, last argument.

So it's NOT that it is not documented, just that the doc maybe assumes too much of the reader, and the lack of any example does not help either.

I just helped a colleague thanks to a post from Keith, and thought maybe Larry (or Richard) might improve the doc to make it more accessible.

My $0.02. Thanks, and happy new year to all. --DD

(2) By ddevienne on 2023-01-04 16:06:20 in reply to 1 [source]

BTW, I also just noticed:

This feature is experimental and is subject to change. Further documentation will become available if and when [...].

Since it's been 5 years, can we assume it's now officially supported? :)

So maybe 2023 is a good time to remove that sentence and add a little more doc?

(3) By ddevienne on 2023-01-05 14:35:16 in reply to 2 [link] [source]

The experimental sentence was removed in https://www.sqlite.org/docsrc/info/bfb7a9010a6f426c by Richard. Thanks.

Hopefully more doc will come eventually.

In the meantime, below's a little demo of the two ways to specify the schema, in case it helps anyone:

C:\Users\ddevienne>sqlite3 foo.db "create table t(u);"

C:\Users\ddevienne>sqlite3 bar.db "create table t(u, v);"

C:\Users\ddevienne>sqlite3
SQLite version 3.36.0 2021-06-18 18:36:39
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> attach 'foo.db' as foo;
sqlite> attach 'bar.db' as bar;
sqlite> .mode box
sqlite> select * from pragma_table_info('t');
┌─────┬──────┬──────┬─────────┬────────────┬────┐
│ cid │ name │ type │ notnull │ dflt_value │ pk │
├─────┼──────┼──────┼─────────┼────────────┼────┤
│ 0   │ u    │      │ 0       │            │ 0  │
└─────┴──────┴──────┴─────────┴────────────┴────┘
sqlite> select * from pragma_table_info('t', 'foo');
┌─────┬──────┬──────┬─────────┬────────────┬────┐
│ cid │ name │ type │ notnull │ dflt_value │ pk │
├─────┼──────┼──────┼─────────┼────────────┼────┤
│ 0   │ u    │      │ 0       │            │ 0  │
└─────┴──────┴──────┴─────────┴────────────┴────┘
sqlite> select * from pragma_table_info('t', 'bar');
┌─────┬──────┬──────┬─────────┬────────────┬────┐
│ cid │ name │ type │ notnull │ dflt_value │ pk │
├─────┼──────┼──────┼─────────┼────────────┼────┤
│ 0   │ u    │      │ 0       │            │ 0  │
│ 1   │ v    │      │ 0       │            │ 0  │
└─────┴──────┴──────┴─────────┴────────────┴────┘
sqlite> select * from pragma_table_info('t') where schema = 'foo';
┌─────┬──────┬──────┬─────────┬────────────┬────┐
│ cid │ name │ type │ notnull │ dflt_value │ pk │
├─────┼──────┼──────┼─────────┼────────────┼────┤
│ 0   │ u    │      │ 0       │            │ 0  │
└─────┴──────┴──────┴─────────┴────────────┴────┘
sqlite> select * from pragma_table_info('t') where schema = 'bar';
┌─────┬──────┬──────┬─────────┬────────────┬────┐
│ cid │ name │ type │ notnull │ dflt_value │ pk │
├─────┼──────┼──────┼─────────┼────────────┼────┤
│ 0   │ u    │      │ 0       │            │ 0  │
│ 1   │ v    │      │ 0       │            │ 0  │
└─────┴──────┴──────┴─────────┴────────────┴────┘
sqlite> select * from pragma_table_info('t', 'foo')
   ...> except
   ...> select * from pragma_table_info('t', 'bar');
sqlite> select * from pragma_table_info('t', 'bar')
   ...> except
   ...> select * from pragma_table_info('t', 'foo');
┌─────┬──────┬──────┬─────────┬────────────┬────┐
│ cid │ name │ type │ notnull │ dflt_value │ pk │
├─────┼──────┼──────┼─────────┼────────────┼────┤
│ 1   │ v    │      │ 0       │            │ 0  │
└─────┴──────┴──────┴─────────┴────────────┴────┘
sqlite>

PS: Our use case was finding schema differences across DBs, FWIW. Thus the EXCEPT queries at the end.