Search code examples
delphisqlitedelphi-xe3firemonkey-fm2

TSQLQuery not returning right amount of records


I have a table called Artist which currently contains four records and a TSQLQuery that contains the following statement:

SELECT name
FROM Artist

The table Artist also contains the following records:

id          name
1           Artist 1
2           Artist 2
3           Artist 3
4           Artist 4

Current method of record retrieval:

Query1.Open;
for i := 0 to qArtist.FieldCount -1 do
    with cbArtist.ListBox.ListItems[i] do
        Text := qArtist.Fields[i].AsString;

Previous method of record retrieval:

Data bind Query1 to ComboBox1.


With the "previous" method, ComboBox1 would display all the expected records from the Artist table. However, when I try to use "current" method Query1 is only selecting the very first record from the Artist table despite there being three other existing values. I have tried the "current" method across other queries and they also returned only the first value of the table.

The reason I am going for this new approach is because I feel that I am very limited in what I can do if I continue to the "previous" / data bind method, but that is besides the point.

So how can I fix this problem? i.e. the problem of the query only selecting the very first record from the table.


Solution

  • You must use the Eof and Next methods to iterate over the records.

    Query1.Open;
     while not Query1.eof do
     begin
      cbArtist.Items.Add(Query1.FieldByName('Artist').AsString);
      Query1.Next;
     end;