Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-01

How to get field-names of a table ?

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Koenraad Lelong

Hi,

I have a tsqlQuery with as sql : "select * from mytable". I don't add the fields at design-time by double-clicking on the query and then adding the fields. I would like to do looked at the properties of the sqlQuery and tried some of the methods, but I can't find anything that works.
I tried in the create of the datamodule :

tmpField:=TStringField.Create(nil);
tmpField.FieldKind:=fkData;
tmpField.FieldName:='COUNT';
MyQuery.Fields.Add(tmpField);
MyQuery.Open;

Destroy of the datamodule :

MyQuery.Close;
tmpField.Free;

This compiles fine and runs fine until I try to use the COUNT-field, which does exist in the table.
Closing the application without using the field also creates a segfault.

Any hints how to do what I want ?
Database = firebird, lazarus = 0.9.30

Thanks,

Koenraad Lelong.

Reply by email

Maximiliano Duarte
to Koenraad Lelong

When you open the query you object is deleted.
If yuo need only a field name is

Query1.sql.text:= "select * from mytable";
Query1.open;
Name:=Query1.Fields[0].fieldname;

Enviado desde mi BlackBerry

Earlier message 38 lines
-----Original Message----- From: Koenraad Lelong Date: Thu, 05 Jan 2012 17:20:04 To: Reply-To: Lazarus mailing list Subject: [Lazarus] How to get field-names of a table ? Hi, I have a tsqlQuery with as sql : "select * from mytable". I don't add the fields at design-time by double-clicking on the query and then adding the fields. I would like to do looked at the properties of the sqlQuery and tried some of the methods, but I can't find anything that works. I tried in the create of the datamodule : tmpField:=TStringField.Create(nil); tmpField.FieldKind:=fkData; tmpField.FieldName:='COUNT'; MyQuery.Fields.Add(tmpField); MyQuery.Open; Destroy of the datamodule : MyQuery.Close; tmpField.Free; This compiles fine and runs fine until I try to use the COUNT-field, which does exist in the table. Closing the application without using the field also creates a segfault. Any hints how to do what I want ? Database = firebird, lazarus = 0.9.30 Thanks, Koenraad Lelong. --

Reply by email

Koenraad Lelong
to Maximiliano Duarte
Maximiliano Duarte wrote 7 lines
When you open the query you object is deleted. If yuo need only a field name is Query1.sql.text:= "select * from mytable"; Query1.open; Name:=Query1.Fields[0].fieldname;

Thanks for your response, but it does not work.

The table I use has more than five fields. I manually created one field (double clicking the sqlQuery and adding one field). When I run the program, I display Query1.FieldCount : 1. When I try to display Query1.Fields[1].fieldname ([1], i.e. the second fieldname) I get an "list index out of bounds" error. Displaying Query1.Fields[0].fieldname gives the fieldname of the field I manually created.

So is there a way to what Lazarus can do (adding fields) by using the properties and/or methods of a TSQLQuery, or another component ?

Thanks for any help,

Koenraad Lelong.

Reply by email

Ludo Brands
to Koenraad Lelong
Show quoted text 10 lines
> When you open the query you object is deleted. > If yuo need only a field name is > > Query1.sql.text:= "select * from mytable"; > Query1.open; > Name:=Query1.Fields[0].fieldname; > Thanks for your response, but it does not work.

It works if you don't define your fields yourself.

Show quoted text 11 lines
The table I use has more than five fields. I manually created one field (double clicking the sqlQuery and adding one field). When I run the program, I display Query1.FieldCount : 1. When I try to display Query1.Fields[1].fieldname ([1], i.e. the second fieldname) I get an "list index out of bounds" error. Displaying Query1.Fields[0].fieldname gives the fieldname of the field I manually created.

If you define one field manually at run-time you have only one field. Opening the dataset won't add the fields automatically once you defined your own. I haven't seen this mentioned in fpc/lazarus docs but in Delphi articles on adding fields at run-time it is stated quite clear: if you want to add fe. a calculated field at run-time to the existing dataset fkData fields, you have to define all these fields before adding the new one. If you have fielddefs for the query you can add the existing fields simply with:
for i:=0 to MyQuery.FieldDefs.Count-1 do

    Field:=MyQuery.FieldDefs[i].CreateField(MyQuery);

If you don't have fielddefs (0 items) and you don' want to add them in the designer then just add a

  SQLQuery1.Active:=true;
  SQLQuery1.Active:=false;

before the above loop to auto fill the fielddefs. For this to work, as for fields, Fielddefs should be an empty array. As soon on element is in fielddefs, the auto fill won't work.

Now, in your previous mail you created the field with

Show quoted text 5 lines
tmpField:=TStringField.Create(nil); tmpField.FieldKind:=fkData; tmpField.FieldName:='COUNT'; MyQuery.Fields.Add(tmpField); MyQuery.Open;

This is wrong and fpc 2.7.1 gives even a run-time error saying that dataset is not set. Replace MyQuery.Fields.Add(tmpField); with tmpField.Dataset:=MyQuery;.

Ludo

Reply by email

Maximiliano Duarte
to Koenraad Lelong: Thanks for your response, but it does not work. The table I use has more than five…

No if you need an new field, use a calcfield. Add this doble click on tsqlobject, new field, put name, in oncalcfield event fill this with anythinks Enviado desde mi BlackBerry

Earlier message 34 lines
-----Original Message----- From: Koenraad Lelong Date: Fri, 06 Jan 2012 15:31:17 To: Reply-To: Lazarus mailing list Subject: Re: [Lazarus] How to get field-names of a table ? On 06-01-12 01:20, Maximiliano Duarte wrote: > When you open the query you object is deleted. > If yuo need only a field name is > > Query1.sql.text:= "select * from mytable"; > Query1.open; > Name:=Query1.Fields[0].fieldname; > Thanks for your response, but it does not work. The table I use has more than five fields. I manually created one field (double clicking the sqlQuery and adding one field). When I run the program, I display Query1.FieldCount : 1. When I try to display Query1.Fields[1].fieldname ([1], i.e. the second fieldname) I get an "list index out of bounds" error. Displaying Query1.Fields[0].fieldname gives the fieldname of the field I manually created. So is there a way to what Lazarus can do (adding fields) by using the properties and/or methods of a TSQLQuery, or another component ? Thanks for any help, Koenraad Lelong. --

Reply by email

Koenraad Lelong
to Ludo Brands: It works if you don't define your fields yourself. If you define one field manually at…
Ludo Brands wrote 7 lines
designer then just add a SQLQuery1.Active:=true; before the above loop to auto fill the fielddefs. For this to work, as for fields, Fielddefs should be an empty array. As soon on element is in fielddefs, the auto fill won't work.

Hi,

Thanks Ludo, opening the query auto-filled the fielddefs, if there were no manually created fielddefs.

Now I have to give each field an onGetText event. I was looking for some documents, but I seem unable to find something. Any help ? I tried several things but only this compiles, and it even works : for i:=0 to MyQuery.FieldDefs.Count-1 do

  ;

I created MyQueryDATAGetText by manually creating a field, creating onGetText by double-clicking on it and putting my code in it. Afterwards I deleted the field.

Is this OK ? To me it seems a hack.

Thanks.

Regards,

Koenraad Lelong

Reply by email

Ludo Brands
to Koenraad Lelong
Show quoted text 17 lines
On 06-01-12 17:19, Ludo Brands wrote: > designer then just add a > SQLQuery1.Active:=true; > before the above loop to auto fill the fielddefs. For this to work, as > for fields, Fielddefs should be an empty array. As soon on element is > in fielddefs, the auto fill won't work. > Hi, Thanks Ludo, opening the query auto-filled the fielddefs, if there were no manually created fielddefs.

That is from inside lazarus. The code snippet did the same at run time.

Show quoted text 13 lines
Now I have to give each field an onGetText event. I was looking for some documents, but I seem unable to find something. Any help ? I tried several things but only this compiles, and it even works : for i:=0 to MyQuery.FieldDefs.Count-1 do ; I created MyQueryDATAGetText by manually creating a field, creating onGetText by double-clicking on it and putting my code in it. Afterwards I deleted the field. Is this OK ? To me it seems a hack.

Normally, when you need to use field events, you would create persistent fields. With Fielddefs populated, double click on the TSQLQuery and click on the + sign. You'll see a list of all fielfs in fielddefs. Select all fields and OK. You have now created persistent fields as opposed to fields created automatically by opening a dataset. All field settings are now permanent, including events assigned to them.

BTW, I would replace
 for i:=0 to MyQuery.FieldDefs.Count-1 do
With
 for i:=0 to MyQuery.Fields.Count-1 do

Ludo

Show quoted text 12 lines
Thanks. Regards, Koenraad Lelong -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Koenraad Lelong
to Ludo Brands
Ludo Brands wrote

...

Show quoted text 6 lines
Normally, when you need to use field events, you would create persistent fields. With Fielddefs populated, double click on the TSQLQuery and click on the + sign. You'll see a list of all fielfs in fielddefs. Select all fields and OK. You have now created persistent fields as opposed to fields created automatically by opening a dataset. All field settings are now permanent, including events assigned to them.

That's the point of my application. It should to be able to select various tables (only one at each time) and then use these tables to create labels from them, using LazReport.
As I experienced with LazReport, there should be fields defined for the used tables, otherwise, you can't use the fields when you design a report. Unfortunately, some of the table's fields contain spaces at the end of the data. Those spaces should be removed. That's what I do in the event.

Now that I'm writing this, maybe I could do this in an event of the report. I'll try this.

Thanks for your help,

Koenraad

Reply by email