PDA

View Full Version : QSqlQuery and non-sql query



zygmunt
19th January 2011, 00:20
Hi!
Is it posible to use non sql query in Qt?
I need to get the result from query: "\d my_table".

PS: i use postgresql, but it will be good trick for other dbms.

wysota
19th January 2011, 00:57
What you mean is not a query but a command of postgresql console client (psql). Try querying for "SHOW CREATE TABLE my_table" or "DESCRIBE my_table" instead. If that's not enough, run psql with -E option and then issue the \d my_table command to see what the SQL query for getting the result is.

ChrisW67
19th January 2011, 03:28
If you are doing this to get the table structure then you should look at QSqlDatabase::tables(), QSqlDatabase::record(), and QSqlDatabase::primaryIndex().

zygmunt
19th January 2011, 10:57
In psql:


create table name(id serial primary key, name varchar(12));
create table other_info(id serial primary key, ref integer references name(id));

zygmunt=> \d other_info
Table "public.other_info"
Column | Type | Modifiers
--------+---------+---------------------------------------------------------
id | integer | not null default nextval('other_info_id_seq'::regclass)
ref | integer |
Indexes:
"other_info_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"other_info_ref_fkey" FOREIGN KEY (ref) REFERENCES name(id)


And i need to parse this line:


"other_info_ref_fkey" FOREIGN KEY (ref) REFERENCES name(id)


How to do it?

wysota
19th January 2011, 11:18
Did you do what I said about the -E switch?

zygmunt
19th January 2011, 18:39
@wysota:
My qustion:
Is it posible to use non sql query in Qt?
Query like: "\d my_table" isn't compatible with SQL standard, for me is query, but non-sql.
Thanks for -E switch.

Now it work, but i have strange value in query:


SELECT conname,
pg_catalog.pg_get_constraintdef(r.oid, true) as condef
FROM pg_catalog.pg_constraint r
WHERE r.conrelid = '19264' AND r.contype = 'f' ORDER BY 1


Thanks anyway, thread is solved,
and i go to search what is it 19264:)

PS: Query like: "SHOW CREATE TABLE my_table", or
"DESCRIBE my_table" doesn't work in postgresql.

wysota
19th January 2011, 18:44
Query like: "\d my_table" isn't compatible with SQL standard, for me is query, but non-sql.
It's not a query. The same as \q which quits the console is not a query. It's a command of the psql program.