Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

sql: get a list of field names from a data table

Hi,

I'm new to sql and wanted help in getting a list of field names (column headers) in a data table.

I really don't have any idea how to do it, though I've searched google and read the documentation.

I am using sqlite and qt, if that matters.

Cheers

Mick
[315 byte] By [eutherian] at [2007-11-11 7:53:11]
# 1 Re: sql: get a list of field names from a data table
Well, almost as soon as I posted my message I worked out how to do it using the Qt sqlite binding:

QSqlQuery q("select * from mytable LIMIT 0, 0"); //don't actually select anything but get a reference to the record set

QSqlRecord rec = q.record();

QStringList fields;

for (int i=0; i<rec.count(); i++)
fields.append(rec.fieldName(i));

//now, fields contains a list of field names

But if there's a pure sql way of doing it I'd love to know!

Cheers

Mick
eutherian at 2007-11-11 23:47:36 >
# 2 Re: sql: get a list of field names from a data table
Looking at your SQL code, your not using SQL Server - SQL Server uses TOP rather than limit. PL-SQL?
The following can be done in SQL Server quite easily - I'm not as familiar with other database products but maybe you can access table metadata using system tables/views the same way you can with SQL Server:

select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'tablename'
Wiseman82 at 2007-11-11 23:48:42 >