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?
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.
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.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With