Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the results of an SQL query?

Using Delphi 2010, I have used TSQLQuery and TSQLConnection to connect to a remote MySQL server. I have used an SQL query as follows:

SQLQuery1.SQL.Text := 'SELECT * FROM registered WHERE email="'+email+'" and login_pass="'+password+'"';

SQLQuery1.Open; // Open sql connection

What should I do to list or display the data selected by this query?

When I type

SQLQuery1['who']; // The resault is : James Kan

I think it is displaying the very last item in the list. But I want to display each item, as I could with the foreach loop in PHP. How could I create, for example, a TLabel for each item?

like image 717
Rafik Bari Avatar asked Dec 16 '11 10:12

Rafik Bari


People also ask

How do I see the results of a SQL query?

You have the option of displaying your query results on the Run SQL window, as opposed to Data Display windows. To do this, go to View > Data Grid (Ctrl+G). Once you have selected this option, a panel will appear at the bottom of the window - your query results will be displayed there.

How do I retrieve data from a query?

In queries where all the data is found in one table, the FROM clause is where we specify the name of the table from which to retrieve rows. In other articles we will use it to retrieve rows from multiple tables. The WHERE clause is used to constrain which rows to retrieve.


2 Answers

You just iterate over the resultset like

SQLQuery1.Open;
SQLQuery1.First; // move to the first record
while(not SQLQuery1.EOF)do begin
   // do something with the current record
   ...
   // move to the next record
   SQLQuery1.Next;
end;
like image 156
ain Avatar answered Oct 13 '22 11:10

ain


You can use a TDBGrid to display the results quite neatly. You need a datasource which links to your query via the DataSet property of the datasource. Then the DBGrid links to the dataSource via the DataSource Property of the DBGrid.

like image 32
Glen Avatar answered Oct 13 '22 11:10

Glen