SQLite Forum

Pragma query with mutiple DBs
Login

Pragma query with mutiple DBs

(1) By oneeyeman on 2022-11-21 05:51:24 [link] [source]

Hi, ALL,

If I work with one DB than in my C++ code I can run

SELECT * FROM pragma_table_info(?)

and everything works fine.

However if I have a second DB ATTACH'ed running

SELECT * FROM <schema>.pragma_table_info(?)

will fail because neither of those DBs have a tale that called "pragma_table_info"

So how do I execute such query against ATTACH'ed DB?

Thank you.

(2) By Ryan Smith (cuz) on 2022-11-21 06:00:16 in reply to 1 [link] [source]

The pragma function belongs to the connection, not the DB schema.

In other words,

in stead of:
SELECT * FROM <schema>.pragma_table_info(<table>);

rather try:
SELECT * FROM pragma_table_info(<schema>.<table>);

(4) By Keith Medcalf (kmedcalf) on 2022-11-21 14:40:37 in reply to 2 [link] [source]

That is incorrect.

To select from the eponymous pragma tables that take an object name and a schema as argument you must present the arg first and the schema second, or use the appropriate WHERE clause names, and the object names must be text strings, not object referents.

select * from pragma_table_info('<table>', '<schema>')

or

select * from pragma_table_info where schema='<schema>' and arg='<table>'

This applies to all virtual tables, whether eponymous or not. The "input" parameters may appear either as arguments to the virtual table name itself or in the where clause.

select value from generate_series where start=1 and stop=10

or equivalently

select value from generate_series(1,10)

(3.1) By Keith Medcalf (kmedcalf) on 2022-11-21 14:42:55 edited from 3.0 in reply to 1 [link] [source]

However if I have a second DB ATTACH'ed running

SELECT * FROM <schema>.pragma_table_info(?)

will fail because neither of those DBs have a tale that called pragma_table_info"

This is an incorrect statement *you obviously did not try it).

"real" objects, which includes virtual tables, exist within the confines of a specific schema -- the one in which the CREATE <whatever> was executed. Specifying a different schema from the one in which the object was created (and in which the object does not exist) will return a object does not exist error.

eponymous virtual tables (such as the pragma tables, generate_series, wholenumber, and so on and so forth) are owned by the connection itself and do not "exist" within the confines of any specific schema (they were not created by executing a CREATE <object> statement) but rather are a creature of the connection. As such any resolvable schema name that exists within the connection may be used when referencing such objects, and all such referents refer to the very self-same eponymous virtual table indifferently to the schema reference used or not used.

(5) By anonymous on 2022-11-21 15:59:11 in reply to 3.1 [link] [source]

any resolvable schema name that exists within the connection may be used when referencing such objects

Not quite any...

sqlite> create temp table pragma_table_info (id integer primary key);
sqlite> select * from pragma_table_info('pragma_table_info');
Parse error: 'pragma_table_info' is not a function
sqlite> select * from main.pragma_table_info('pragma_table_info');
+-----+------+---------+---------+------------+----+
| cid | name |  type   | notnull | dflt_value | pk |
+-----+------+---------+---------+------------+----+
| 0   | id   | INTEGER | 0       |            | 1  |
+-----+------+---------+---------+------------+----+
sqlite> select * from main.pragma_table_info('pragma_table_info','main');
+-----+------------+------+---------+------------+----+
| cid |    name    | type | notnull | dflt_value | pk |
+-----+------------+------+---------+------------+----+
| 0   | cid        |      | 0       |            | 0  |
| 1   | name       |      | 0       |            | 0  |
| 2   | type       |      | 0       |            | 0  |
| 3   | notnull    |      | 0       |            | 0  |
| 4   | dflt_value |      | 0       |            | 0  |
| 5   | pk         |      | 0       |            | 0  |
+-----+------------+------+---------+------------+----+

(6) By Keith Medcalf (kmedcalf) on 2022-11-21 16:42:53 in reply to 5 [link] [source]

You are confounding yourself by creating an object with the same name as an eponymous virtual table which will, of course, change the search rules.

(7) By anonymous on 2022-11-21 17:02:20 in reply to 6 [link] [source]

When you are not in control of the schema, assume malice and code defensively.

(8) By Keith Medcalf (kmedcalf) on 2022-11-21 17:32:10 in reply to 7 [source]

Actually, I was in error. No error is thown if a non-existant schema name is used. If no schema is specified then the standard search order is used.

SQLite version 3.40.0 2022-11-16 12:10:08
Enter ".help" for usage hints.
sqlite> .databases
main: "" r/w
sqlite> create temporary table pragma_table_list(x);
sqlite> insert into pragma_table_list values (1);
sqlite> select * from pragma_table_list;
1
sqlite> .databases
main: "" r/w
temp: "" r/w
sqlite> select * from eponymous.pragma_table_list;
main|sqlite_schema|table|5|0|0
temp|pragma_table_list|table|1|0|0
temp|sqlite_temp_schema|table|5|0|0
sqlite> select * from eponymous.pragma_table_list('temp');
sqlite> select * from eponymous.pragma_table_list where schema='temp';
temp|pragma_table_list|table|1|0|0
temp|sqlite_temp_schema|table|5|0|0
sqlite>