Recent

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

Milsa

  • Sr. Member
  • ****
  • Posts: 261
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.0.10, FPC 3.2.0, SVN 63526
This information is actual to: 1st Aug 2020

dseligo

  • Sr. Member
  • ****
  • Posts: 360
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: 739
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 IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

Milsa

  • Sr. Member
  • ****
  • Posts: 261
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.0.10, FPC 3.2.0, SVN 63526
This information is actual to: 1st Aug 2020

 

TinyPortal © 2005-2018