Recent

Author Topic: SQLite: Get field names from table  (Read 1816 times)

Milsa

  • Sr. Member
  • ****
  • Posts: 309
SQLite: Get field names from table
« on: September 27, 2021, 07:54:59 pm »
I have this fragment of code:
Code: Pascal  [Select][+][-]
  1.     SQL.Text := Format('SELECT * FROM [%s]', [Name]);
  2.     Open;
  3.     while not EOF do
  4.     begin
  5.       //detecting field names
  6.       Next;
  7.     end;
  8.     Close;      
Can I detect field names from TSQLQuery like "Fields[0].Name"?
I work with Lazarus 2.2.2, FPC 3.2.2, date 2022-05-15
This information is actual to: 28st Dec 2022

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: SQLite: Get field names from table
« Reply #1 on: September 28, 2021, 07:49:52 am »
This works with ZEOSlib:
Code: Pascal  [Select][+][-]
  1. For i:=0 to Fields.Count-1 do
  2.   showMessage(Fields[i].FieldName);

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: SQLite: Get field names from table
« Reply #2 on: September 28, 2021, 08:39:06 am »
Use "GetFieldNames" of TSQLite3Connection of TSQLConnection (SQLite3Connection inherits from it) --> TSQLite3Connection overrides the one from TSQLConnection
https://www.freepascal.org/daily/packages/fcl-db/sqlite3conn/tsqlite3connection.getfieldnames.html
https://www.freepascal.org/docs-html/fcl/sqldb/tsqlconnection.getfieldnames.html

EDIT: Alternative is to fire a PRAGMA-Call from a TSQLQuery-Object
Code: Pascal  [Select][+][-]
  1. MySQLQuery.SQL.Text:='PRAGMA table_info(MyTableName)';
  2. MySQLQuery.Open;
« Last Edit: September 28, 2021, 11:07:16 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Milsa

  • Sr. Member
  • ****
  • Posts: 309
Re: SQLite: Get field names from table
« Reply #3 on: October 02, 2021, 01:40:49 pm »
Thank you very much. This is solution for me.
I work with Lazarus 2.2.2, FPC 3.2.2, date 2022-05-15
This information is actual to: 28st Dec 2022

 

TinyPortal © 2005-2018