Sintax error at or near "Select"

Asked

Viewed 50 times

0

I am trying to display the contents of the Postgressql database with the employee call table, using the Botton and Bdgrid components of Delphi to display, I entered the commands like this:

untDMPrincipal.DMPrincipal.UniQuery2.Close;
untDmPrincipal.DMPrincipal.UniQuery2.SQL.Add('Select * from funcionarios');
untDmPrincipal.DMPrincipal.UniQuery2.Open;

Data is normally displayed whenever I press the Botton, but when I click a second time on the "Show Data" button Delphi reports an error:

Sintax error at or near "Select"

  • the Add method must be called every click, causing the query to be 'Select * from funcionariosSelect * from funcionarios' in the second... and successively... you have to add only once, or clear before adding to each click

  • If you put a ; at the end of the command SELECT it will not give error but will repeat the display as many times as are the clicks.

  • There is no way to do so: "untDmPrincipal.DMPrincipal.Uniquery2.SQL.Text := 'Select * from employees';"

  • Ai instead of adding the string he subscribes it

  • 1

    Put the ; Solved

3 answers

1

Try to clear SQL from your query first..

Query.SQL.Clear;

Or instead of using the Add function use Text:

Query.SQL.Text := '';

0

Each time the button is clicking a new line is being added to the query. Sql.Add does not delete the previous text, it just adds the new text to the end of the Stringlist.

Put this code in the Onclick button, click twice or more times and see how your SQL looks: ShowMessage(untDmPrincipal.DMPrincipal.UniQuery2.SQL.Text)

@Gabriellocalhost has already given the answer:

untDmPrincipal.DMPrincipal.UniQuery2.SQL.Text := 'Select * from funcionarios'

0

The correct thing is to clear the list before trying to add a new query. The estate SQL is probably derived from a TList, so use the command Clear.

untDMPrincipal.DMPrincipal.UniQuery2.Close;
untDmPrincipal.DMPrincipal.UniQuery2.SQL.Clear;
untDmPrincipal.DMPrincipal.UniQuery2.SQL.Add('Select * from funcionarios');
untDmPrincipal.DMPrincipal.UniQuery2.Open;

Placing the semicolon DOES NOT SOLVE the problem, it will just cover the sun with the sieve. This way it would be running multiple queries. For the database will arrive:

Select * from funcionarios;Select * from funcionarios;Select * from funcionarios....

he will understand and execute all queries and you will have access only to the last, however, will be open transactions for each cursor he used.

  • I particularly don’t like to use this form because it adds an extra line in the code, one more command to be done, I prefer not to use Clear and Add, I use Text that it subscribes to sql

Browser other questions tagged

You are not signed in. Login or sign up in order to post.