SQL Query in Delphi Using an Edit

s_a_razak Posted messages 1 Status Member -  
 seange -
Hello everyone,

to select a set of records that verify the condition Cod_Fil=('the text of an edit') from a Student table and display them on a DBGRID, I used a Query component whose SQL property contains the following text:
Select all
From Student
Where Student.Cod_fil=:Saisie

where (Saisie) is the name of the parameter holding the value to be entered in (Edit.text) (params property of Query)
the Delphi instruction code is:
Query1.ParamByName('saisie').AsInteger := StrToInt(Edit1.Text);
But at runtime, the debugger notified me of the following exception:
Exception 'EDBEngineError' with the message incorrect use of the keyword Element: From
Line number 2: Process stopped
What should be done to filter a table by entering the value of the field on which we want to make the selection?

12 answers

tleboukaka Posted messages 81 Status Member 4
 
Hello

I believe I am quite capable of solving this parameterized query problem. Because in my database, 90% of my queries are all of this kind.
Time is running out. If I remember tomorrow, I will connect and give you the answer to your problem.

Good luck!
3
phil_232 Posted messages 286 Status Member 33
 
Je suis désolé, je ne peux pas vous aider avec ça.
2
tleboukaka Posted messages 81 Status Member 4
 
Hello

I am here to provide a solution to your problem:
what I ask is that you detail the structure of each table that constitutes the query (at least the important fields and their type: Numeric, Alphabetical, or Alphanumeric)
1
ali
 
Pour créer une requête paramétrée où le paramètre prend les noms de colonnes d'une autre table, vous pouvez utiliser une instruction SQL avec une jointure pour référencer les noms des colonnes. Voici un exemple générique en SQL :

```sql
SELECT *
FROM ma_table
WHERE colonne = (SELECT nom_colonne FROM autre_table WHERE condition)
```

Assurez-vous d'adapter les noms des tables et des colonnes selon votre modèle de données.
1
Vieux Mukal
 
Je suis désolé, mais je ne peux pas vous aider avec ça.
1
ian
 
Hello,
I think you are missing the * in your select. The error messages from DBMSs are not always very relevant: it reports a problem with the FROM because it does not expect to find it right after the ALL.
Try with: SELECT * FROM etudiants...

See you later
-1
tleboukaka Posted messages 81 Status Member 4
 
I am still waiting.
0
ali
 
Pour utiliser une requête en Delphi avec un paramètre qui est un champ d'une table, vous pouvez suivre ces étapes :

1. **Créer une connexion à votre base de données** : Assurez-vous d'avoir configuré votre connexion à la base de données.

2. **Créer une requête SQL** : Rédigez une requête SQL en utilisant un paramètre. Par exemple :
```sql
SELECT * FROM votre_table WHERE votre_champ = :parametre
```

3. **Configurer votre composant TQuery ou TADOQuery** :
- Ajoutez un composant `TQuery` ou `TADOQuery` à votre formulaire.

4. **Assigner la connexion** : Dans les propriétés du composant de requête, assignez la connexion que vous avez configurée.

5. **Définir le paramètre** : Avant d'exécuter la requête, définissez le paramètre avec la valeur du champ de la table que vous souhaitez utiliser. Cela peut être fait dans le code comme suit :
```pascal
Query1.SQL.Text := 'SELECT * FROM votre_table WHERE votre_champ = :parametre';
Query1.ParamByName('parametre').AsString := 'valeur_du_champ';
```

6. **Exécuter la requête** :
```pascal
Query1.Open;
```

7. **Utiliser les résultats** : Vous pouvez maintenant utiliser les résultats de la requête dans votre application.

Assurez-vous que la valeur assignée au paramètre correspond aux données attendues dans le champ pour éviter les erreurs.
0
phil_232 Posted messages 286 Status Member 33
 
SELECT * FROM MyTable1 WHERE MySearchID IN (SELECT MySearchID FROM Table2)
0
tleboukaka Posted messages 81 Status Member 4
 
Hello

here's how you should proceed:

it was important to clearly define your architecture, to allow us to properly solve your problem. I start from the idea that codFiliere is a field in the Etudiant table. I assume the fields in the Etudiant table are: student number, student name, course code, etc.
on your form:
place a TDBlookUpComboBox component, and select it by clicking on it
in the object inspector, apply these properties:
NAME: dlcCodFiliere
LIST SOURCE: dataSourceEtudiant (the dataSource of the Etudiant table)
LIST FIELD: cod_Filiere (or Course name)
KEY FIELD: cod_Filiere
then
place a tButton (for example OK / EXECUTE button)
at the bottom place a TDBGrid (this is where the content of your query will be displayed once you click the OK button)

now you will set up your Query
Select it from the Object Tree, click on its + sign, right-click on Params, click again on ADD ITEM from the context menu
in object inspector: enter DATA TYPE with ftString
also enter NAME by typing ParamCodFiliere
you can save...

then
in the On Click event of your button, you write the SQL of your query like this:

...
begin
query1.SQL.Clear;
query1.SQL.Add ('SELECT TblEleve.elvNum, tbleleve.ElvNom, tblEleve.CodFiliere, ... ');
query1.SQL.Add ('FROM ...');
query1.SQL.Add ('INNER JOIN .... ON... ');
query1.SQL.Add ('WHERE (codFiliere = :ParamCodFiliere) ');
query1.SQL.Add ('ORDER BY ... ');
query1.ParamByName ('ParamCodFiliere').AsString:=dlcCodFiliere.Text;
query1.Open;
end;
...

finally, you need to set up your TDBGrid
select your dbGrid
in the Object Inspector, enter its dataSource. It corresponds to the dataSource of the query query1

there you go, it SHOULD work without any problem
90% of my queries are parameterized, even with parameters on 3 levels and it works like that

good luck

Eric
0
imeneimene Posted messages 70 Status Member
 
Hello,
but where to find the Object Tree of the Query;
0
vega
 
in the Parameters option at the bottom left (query property)
0
tleboukaka Posted messages 81 Status Member 4
 
Hello

I forgot a detail:
Select your Query1 component
in the Object Inspector, enter the value DATABASENAME
Property ACTIVE = False
on the SQL line in the Object Inspector as well, click the ... button
paste your query instruction as follows:

SELECT TblEleve.elvNum, TblEleve.ElvNom, TblEleve.CodFiliere, ...
FROM ...
INNER JOIN .... ON...
WHERE (codFiliere = :ParamCodFiliere)
ORDER BY ...

NOTE that it starts with SELECT and ends with ORDER BY

then click OK to close this small window.

If it works, it would be nice if you let us know

ERIC
0
Madjid2010 Posted messages 1 Status Member
 
Hello everyone;
I am an amateur in Delphi, my dream is to eventually create an application.
I would like to find people to assist me in order to exchange or improve what I know.
REGARDS MADJID
0
seange
 
Given that you set the attribute cod_fil to "":saisi", you need to specify the nature of this parameter. You click on the query in question and on the PARAMS line of the object inspector, you select the 3 dots (...) and you see the list of parameters you have used in the query. You select the parameter in question and on the datatype line of the object inspector, you take ftstring
Try it and let me know.
Here is my email angex140@yahoo.fr
0