Page 1 of 1

How to get a single value from MySQL(executescalar in .NET)?

Posted: 14.10.2010, 16:34
by sheridan101
Hi all.

Although I have extensive programming experience I am new to Delphi (ver. 7) and Zeos.
More or less I was able to figure out how to use Zeos objects and how to link them to controls etc.

What I was unable to figure is how to use command in SQL language to find and get single value from some table.
For example.
I want to use SQL command:

Code: Select all

SELECT UserID FROM Users WHERE FirstName = 'John'
and to get single integer value.
Up to now I executing SQL commands trough ZSQLProcessor and this works fine but there is no returning value.

I hope the questions is not too stupid :cry:

Thanks in advance.

Posted: 19.10.2010, 18:38
by guidoaerts
Put a TZQuery on your form, fill in the zconnection and the sql and open it.
If you add a TDatasource and TDBGrid, you see the result in designtime.
guido

Posted: 08.11.2010, 01:45
by mdaems
sheridan101,
You have some options.
The most efficient is using the dbc layes directly, as is done in examples/dbc. However this is a little bit too 'specialised'.
The simplest way is dropping a TZConnection and a TZQuery on your form. Open the connection and the query and then read the first field of the current record.
Basically (but NOT tested):

Code: Select all

ZQuery1.sql.text := <your query>;
ZQuery1.Connection := Connection1;
ZQuery1.open;
l_result := ZQuery1.Fields[0].AsString;
Mark