Bonjour,

Envoyé par
colonel098
J'utilise comme requêtes
INSERT INTO PERSONNEL (IDPERS,NOMPERS, PRENOMSPERS) VALUES (IDPERS,NOMPERS, PRENOMSPERS)
il manque déjà les ':' pour indiquer les paramètres le bon SQL aurait été
INSERT INTO PERSONNEL (IDPERS,NOMPERS, PRENOMSPERS) VALUES (:IDPERS,:NOMPERS,:PRENOMSPERS)
je présume que IDPERS est un index unique ? peut être même primaire ?
ensuite outre le fait qu'il est toujours plus agréable d'avoir le code balisé (le bouton # permet d'indiquer un bloc de code)
il y a déjà des erreurs dont le fait que si la query a déjà été préparée que se passe t-il ?
voici une manière de faire
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
AQuery:=TFDQuery.Create(nil);
try
with FDQuery do
begin
Connexion:=FDConnexion;
// gestion transaction si besoin
SQL.Text:='INSERT INTO PERSONNEL (IDPERS,NOMPERS, PRENOMSPERS) VALUES (:IDPERS,:NOMPERS,:PRENOMSPERS)';
ParamByName('IDPERS').asString:=Edit1.Text;
ParamByName('NOMPERS').asString:=Edit2.Text;
ParamByName('PRENOMPERS').asString:=Edit3.Text;
try
ExecSQL;
// commit de la transaction
except
on E:Exception do begin
// gestion erreur
// rollback de la transaction
end;
end;
end;
finally
FDQuery.Free;
end; |
Puisqu'il s'agit certainement de Firedac au vu du nom il y a aussi d'autres manières
Directe :
1 2
|
FDConnexion.ExecSQL('INSERT INTO PERSONNEL (IDPERS,NOMPERS, PRENOMSPERS) VALUES (:I,:N,:P)',[Edit1.Text,Edit2.Text,Edit3.Text]); |
moins directe avec un ensemble (fdQuery) déjà existante 'SELECT IDPERS,NOMPERS, PRENOMSPERS FROM PERSONNEL' et un fdUPDateSQL bien renseigné (en utilisant les wizards *) pour le faire "à la manière BDE" avec
* au passage profitez en pour voir les différents SQL proposés
1 2 3 4 5
| FDQUery.Insert; // ou Append
FDQuery.FieldByName('IDPERS').asString:=Edit1.Text;
FDQuery.FieldByName('NOMPERS').asString:=Edit2.Text;
FDQuery.FieldByName('PRENOMPERS').asString:=Edit3.Text;
FdQuery.Post; |
un data source,ou en option des liveBindings ,une bonne gestion des évènements onxxxxError ajoutée, des DBEdit pour remplacer les Editx.text
et on arrive à faire du code robuste
Partager