Butanium
Butanium

Reputation: 790

How to get the primary keys column of a table in SQLite

In my exercise sheet, my teacher use a python interface with SQLite. But I preferred to work with SQLite on Datagrip.

I'm asked to write a function which take as argument a table and returns the primary key column. I found that writing function in SQLite wasn't possible and that I needed some php interface or whatever.

Anyway as far as it's just training to get some SQL knowledge I tried to write a query for a table which returns a table containing the primary keys of a table.

Using the PRAGMA table_info (Evenements) query I get this table : enter image description here

So I copied it in a Test table and wrote this query which works :

SELECT name from Test where pk;

But I can't replace Test by PRAGMA table_info (Evenements) or table_info (Evenements).

Do you know if I can directly use the table_info result without having to copy it in a new table?

Upvotes: 3

Views: 2238

Answers (1)

forpas
forpas

Reputation: 164089

The function that you should use is pragma_table_info():

SELECT * 
FROM pragma_table_info('Evenements')

You will get info about all the columns of the table Evenements.
If you want info only for the primary key add a WHERE clause:

WHERE pk 

Upvotes: 4

Related Questions