IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 Delphi Discussion :

Modifier ou Ajouter un enregistrement par une requête SQL


Sujet :

Delphi

  1. #1
    Nouveau membre du Club
    Inscrit en
    Août 2007
    Messages
    159
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 159
    Points : 37
    Points
    37
    Par défaut Modifier ou Ajouter un enregistrement par une requête SQL
    le code pour l'ajoute marche très bien si l'enregistrement n'existe pas on l'ajoute mais si l'enregistrement existe et différent on doit le maitre ajoure

    le code de modification ou de la mise ajour ne marche pas j'ai des erreurs de syntaxe c'est le code apres le ELSE

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    ZQuery1.SQL.Text:='INSERT INTO tblSystems(NAME,PROVIDERID,KEYINDEX,KEY,COMMENTS) VALUES (:pN,:pP,:pKI,:pK,:pC)';
    if not Ztable2.locate('Name;ProviderID;KeyIndex;key',VarArrayOf([System1,provider1,index1,key1]),[])
    then
    begin
                    // ajouter les données
    ZQuery1.Connection.StartTransaction;
    ZQuery1.ParamByName('pN').Value := System1;
    ZQuery1.ParamByName('pP').Value := provider1;
    ZQuery1.ParamByName('pKI').Value := index1;
    ZQuery1.ParamByName('pK').Value := key1;
    ZQuery1.ParamByName('pC').Value :=DateTimeToStr(now);
    ZQuery1.ExecSQL;
    end else
                 // Maitre ajour les données
    if Ztable2.locate('Name;ProviderID;KeyIndex,key',VarArrayOf([System1,provider1,index1,key1]),[])
    then if Ztable2.FieldByName('Name').Value <> key1
    then
    begin
    ZQuery1.close;
    ZQuery1.sql.clear;
    ZQuery1.SQL.Add('UPDATE tblSystems');
    ZQuery1.SQL.Add('SET KEY= :key1');
    ZQuery1.SQL.Add('WHERE Name = :System1 , ProviderID = :provider1 , KeyIndex = :index1');
    ZQuery1.ExecSQL;
    end;
    de l'aide svp et merci d'avance

  2. #2
    Modérateur
    Avatar de Rayek
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2005
    Messages
    5 235
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 235
    Points : 8 504
    Points
    8 504
    Par défaut
    Tu ne renseignes pas les paramètres de ta query c'est normal que cela ne fonctionne pas (:key1, :system1, etc ...)

  3. #3
    Modérateur
    Avatar de tourlourou
    Homme Profil pro
    Biologiste ; Progr(amateur)
    Inscrit en
    Mars 2005
    Messages
    3 876
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Biologiste ; Progr(amateur)

    Informations forums :
    Inscription : Mars 2005
    Messages : 3 876
    Points : 11 363
    Points
    11 363
    Billets dans le blog
    6
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    if Ztable2.locate('Name;ProviderID;KeyIndex;key',VarArrayOf([System1,provider1,index1,key1]),[])
    then begin // update 
      if Ztable2.FieldByName('Name').Value <> key1
      then begin
        ZQuery1.close;
        ZQuery1.sql.clear;
        ZQuery1.SQL.Add('UPDATE tblSystems');
        ZQuery1.SQL.Add('SET KEY= :key1');
        ZQuery1.SQL.Add('WHERE Name = :System1 , ProviderID = :provider1 , KeyIndex = :index1');
        // il manque la définition des valeurs des paramètres ?
        ZQuery1.ExecSQL;
      end;
    end
    else begin // insert
      ZQuery1.Connection.StartTransaction;
      ZQuery1.SQL.Text:='INSERT INTO tblSystems(NAME,PROVIDERID,KEYINDEX,KEY,COMMENTS) VALUES (:pN,:pP,:pKI,:pK,:pC)';
      ZQuery1.ParamByName('pN').Value := System1;
      ZQuery1.ParamByName('pP').Value := provider1;
      ZQuery1.ParamByName('pKI').Value := index1;
      ZQuery1.ParamByName('pK').Value := key1;
      ZQuery1.ParamByName('pC').Value :=DateTimeToStr(now);
      ZQuery1.ExecSQL;
    end;
    Pour la syntaxe de la clause UPDATE, il faut sûrement plutôt
    UPDATE table SET a=1, b=2 WHERE k=key1
    ou
    UPDATE table SET a=1 WHERE k=key1 and n=name1 (aux parenthèses près)

  4. #4
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 141
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 141
    Points : 41 308
    Points
    41 308
    Billets dans le blog
    63
    Par défaut
    De plus que le SQL est incorrect dans sa clause where!!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    ZQuery1.SQL.Add('UPDATE tblSystems');
    ZQuery1.SQL.Add('SET KEY= :key1');
    ZQuery1.SQL.Add('WHERE Name = :System1 AND ProviderID = :provider1 AND KeyIndex = :index1');

  5. #5
    Nouveau membre du Club
    Inscrit en
    Août 2007
    Messages
    159
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 159
    Points : 37
    Points
    37
    Par défaut
    j'ai modifier comme ça et j'ai toujours la même erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    if Ztable2.locate('Name;ProviderID;KeyIndex,key',VarArrayOf([System1,provider1,index1,key1]),[])
    then if Ztable2.FieldByName('Name').Value <> key1
    then
    begin
    ZQuery1.close;
    ZQuery1.sql.clear;
    ZQuery1.SQL.Add('UPDATE tblSystems SET KEY= :QuotedStr(key1)WHERE Name = :QuotedStr(System1) , ProviderID = :QuotedStr(provider1) , KeyIndex = :QuotedStr(index1)');
    ZQuery1.ExecSQL;
    end;


  6. #6
    Modérateur
    Avatar de Rayek
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2005
    Messages
    5 235
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 235
    Points : 8 504
    Points
    8 504
    Par défaut
    Avant de continuer je te conseil quand même de lire la section

    Débuter avec le SQL

    Puis de lire les tutos sur Delphi et les bases de donnes pour voir le fonctionnement des paramètres ou de chercher sur le forum, il y a une foule d'exemple (Paramètres que tu utilises dans la partie Insert de ton code)

  7. #7
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    707
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 707
    Points : 777
    Points
    777
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ZQuery1.SQL.Add('UPDATE tblSystems SET KEY= :QuotedStr(key1)WHERE Name = :QuotedStr(System1) , ProviderID = :QuotedStr(provider1) , KeyIndex = :QuotedStr(index1)');
    Faut pas mettre de code Delphi dans les requêtes-mêmes !

  8. #8
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 141
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 141
    Points : 41 308
    Points
    41 308
    Billets dans le blog
    63
    Par défaut
    Citation Envoyé par SergioMaster Voir le message
    De plus que le SQL est incorrect dans sa clause where!!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    ZQuery1.SQL.Add('UPDATE tblSystems');
    ZQuery1.SQL.Add('SET KEY= :key1');
    ZQuery1.SQL.Add('WHERE Name = :System1 AND ProviderID = :provider1 AND KeyIndex = :index1');
    je me cite , et puisqu'il faut mâcher le travail avant le execsql il ne faut pas oublier
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    ZQuery1.paramByName('Key1').asxxxx :=Key1
    ZQuery1.paramByName('System1').asxxxx :=System1
    ZQuery1.paramByName('Provider1').asxxxx :=Key1
    ZQuery1.paramByName('KeyIndex').asxxxx :=index1
    plutôt que d'utiliser des .Value , il vaut mieux mettre le type d'où mes asxxxx
    dans le code je crois voir qu'il s'agit uniquement de AsString;

    il y a encore une erreur dans le locate
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    if Ztable2.locate('Name;ProviderID;KeyIndex;key',VarArrayOf([System1,provider1,index1,key1]),[])

  9. #9
    Nouveau membre du Club
    Inscrit en
    Août 2007
    Messages
    159
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 159
    Points : 37
    Points
    37
    Par défaut
    @SergioMaster

    Merci j' ai corrigé exactement comme tu le dit il n y a plus d'erreur maintenant

    mais il ne met ajour aucun enregistrement ,j'ai mis des arrêt pour voir si il exécute les requête oui il les exécutée ,,peut être que le problème et dans les composants zeolib ?

  10. #10
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    707
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 707
    Points : 777
    Points
    777
    Par défaut
    Si tu es toujours avec SQLite, au lieu de faire un...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    if Ztable2.locate ... then begin
      // SQL UPDATE
    end
    else begin
     // SQL INSERT
    end;
    ...tu peux simplifier avec un UPDATE suivi d'un INSERT du genre (je n'ai pas analysé tes requêtes en profondeur, il faut sûrement adapter un peu):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    // Fais la mise à jour uniquement si l'enregistrement existe dans la BDD:
    ZQuery1.SQL.Clear;
    ZQuery1.SQL.Add('UPDATE tblSystems');
    ZQuery1.SQL.Add('SET KEY= :key1');
    ZQuery1.SQL.Add('WHERE Name = :System1 AND ProviderID = :provider1 AND KeyIndex = :index1');
    ZQuery1.paramByName('Key1').AsString := Key1
    ZQuery1.paramByName('System1').AsString := System1
    ZQuery1.paramByName('Provider1').AsString := Key1
    ZQuery1.paramByName('KeyIndex').AsString := index1
    ZQuery1.ExecSql;
     
    // Fais une insertion uniquement si l'enregistrement n'existe pas dans la BDD
    ZQuery1.SQL.Text:='INSERT INTO tblSystems(NAME,PROVIDERID,KEYINDEX,KEY,COMMENTS) VALUES (:pN,:pP,:pKI,:pK,:pC)';
    ZQuery1.SQL.Add('WHERE NOT EXISTS (');
    ZQuery1.SQL.Add(' SELECT 1 FROM tblSystems');
    ZQuery1.SQL.Add('  WHERE Name = :pN1 AND ProviderID = :pP1 AND KeyIndex = :pKI1 AND key = :pK1');
    ZQuery1.SQL.Add(')');
    ZQuery1.paramByName('pN').AsString := ...
    ZQuery1.paramByName('pP').AsString := ...
    ZQuery1.paramByName('pKI').AsString := ...
    ZQuery1.paramByName('pK').AsString := ...
    ZQuery1.paramByName('pN1').AsString := ...
    ZQuery1.paramByName('pP'1).AsString := ...
    ZQuery1.paramByName('pKI1').AsString := ...
    ZQuery1.paramByName('pK1').AsString := ...
    ZQuery1.ExecSQL;

  11. #11
    Nouveau membre du Club
    Inscrit en
    Août 2007
    Messages
    159
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 159
    Points : 37
    Points
    37
    Par défaut
    Merci GoustiFruit

    on fait j'ai désactivé le code de l'insert pour ne testé que le code de l'UPDATE

    je voudrai savoir si il faut ajouter le
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    .
    .
    .
    ZQuery1.Connection.StartTransaction
    .
    .
    .
    ZQuery1.Prepare
    .
    .
    dans ce cas ou non ?

  12. #12
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 141
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 141
    Points : 41 308
    Points
    41 308
    Billets dans le blog
    63
    Par défaut
    Non , je ne pense pas que les ZeosLib soit le problème (sauf peut être en ce qui concerne ta compréhension de ceux ci) .

    Le prepare , permettrait d'augmenter un peu la rapidité de la requête , mais pas dans ce contexte ou le SQL est récréer à chaque passage dans la boucle

    le Starttransaction ? oui , à l'occasion aussi mais sans oublier le commit . Si la connexion a été créer de base , le mode étant en autocommit je ne vois pas trop l'intérêt (enfin dans le contexte)

    Enfin pour ce que tu veux faire , comme Goustifruit le suggère une autre approche est envisageable (bon je ne connait pas mySQL , ni l'objectif précis de ce bout de code) mais un LOCATE dans une table entière de mon point de vue (réseau etc ...) c'est un peu lourd .

  13. #13
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    707
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 707
    Points : 777
    Points
    777
    Par défaut
    Ah oui bien vu Sergio, si tu as mis un StartTransaction avant la requête, il faut faire un Commit à la fin. La transaction n'est pas vraiment utile si tu n'as qu'un enregistrement à modifier ou insérer à la fois.

  14. #14
    Nouveau membre du Club
    Inscrit en
    Août 2007
    Messages
    159
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 159
    Points : 37
    Points
    37
    Par défaut
    bonjour a vous tous et merci de vos conseils
    je n'ai plus d'erreur et la Mise ajour et l'insertion ce fait correctement voici le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    if (Ztable2.locate('Name;ProviderID;KeyIndex',VarArrayOf([System1,provider1,index1]),[]))
    and
    (Ztable2.FieldByName('Key').AsString <> key1)
    then
    begin
    ZQuery1.close;
    ZQuery1.sql.clear;
    ZQuery1.SQL.Add('UPDATE tblSystems');
    ZQuery1.SQL.Add('SET KEY = :key1');
    ZQuery1.SQL.Add('WHERE Name = :System1 AND ProviderID = :provider1 AND KeyIndex = :index1');
    ZQuery1.paramByName('Key1').AsString :=Key1 ;
    ZQuery1.paramByName('System1').AsString :=System1 ;
    ZQuery1.paramByName('Provider1').AsString :=Provider1 ;
    ZQuery1.paramByName('index1').AsString :=index1;
    ZQuery1.ExecSQL;
    end else
     
    ZQuery1.SQL.Text:='INSERT INTO tblSystems(NAME,PROVIDERID,KEYINDEX,KEY,COMMENTS) VALUES (:pN,:pP,:pKI,:pK,:pC)';
    if not Ztable2.locate('Name;ProviderID;KeyIndex',VarArrayOf([System1,provider1,index1]),[])
    then
    begin
                    // ajouter les données
    ZQuery1.Connection.StartTransaction;
    ZQuery1.ParamByName('pN').Value := System1;
    ZQuery1.ParamByName('pP').Value := provider1;
    ZQuery1.ParamByName('pKI').Value := index1;
    ZQuery1.ParamByName('pK').Value := key1;
    ZQuery1.ParamByName('pC').Value :=DateTimeToStr(now);
    ZQuery1.ExecSQL;
    end ;
    end;
    @SergioMaster pour le LOCATE j'ai déjà pausé la question ici http://www.developpez.net/forums/d13...table-3-index/
    ils dise que c'est la meilleur façon !!!! dans mon encien base j’utilisai le FINDKEY je ne trouve pas l’équivalent pour SQLITE

  15. #15
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 141
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 141
    Points : 41 308
    Points
    41 308
    Billets dans le blog
    63
    Par défaut
    Citation Envoyé par Night_Wolf1619 Voir le message
    @SergioMaster pour le LOCATE j'ai déjà pausé la question ici http://www.developpez.net/forums/d13...table-3-index/
    ils dise que c'est la meilleur façon !!!! dans mon encien base j’utilisai le FINDKEY je ne trouve pas l’équivalent pour SQLITE
    Pour le Locate (ou le Findkey d'ailleurs) je m'explique . Je suis dans un environnement de plus en plus réseau distant (réseau local + connexions via internet) à la base de données , je trouve que moins il en passe par le réseau , moins c'est lourd ! Pour faire un Locate il faut que toute la table soit passée en mémoire . C'est toujours pour cela que je préfère ne prendre que les infos nécessaires et donc passé par une Query

    Pour ton morceau de code , je ne vois pas le commit !

  16. #16
    Nouveau membre du Club
    Inscrit en
    Août 2007
    Messages
    159
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 159
    Points : 37
    Points
    37
    Par défaut
    @SergioMaster
    moi mon application et sur mono poste pas de réseau pas de connexion pour le moment (est ce que la locate et la plus rapide dans ce cas ou non ?)

    pour le commit elle est auto avac les composant zeolib il y a une propriété autocomit a true

    et merci d'avance

  17. #17
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 141
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 141
    Points : 41 308
    Points
    41 308
    Billets dans le blog
    63
    Par défaut
    pour le commit elle est auto avac les composant zeolib il y a une propriété autocomit a true
    Pour en revenir au code , le StartTransaction (ligne23) est donc inutile

    je reste persuadé qu'il y possibilité d'un code plus efficace (c'est mon coté perfectionniste) mais faute de renseignements il est difficile d'en être sur .
    J'ai l'impression qu'il y a une boucle utilise ce code ?

  18. #18
    Nouveau membre du Club
    Inscrit en
    Août 2007
    Messages
    159
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 159
    Points : 37
    Points
    37
    Par défaut
    @SergioMaster

    oui il y a une boucle dans mon application c'est juste une boucle qui parcoure un fichier texte et lie les lignes car ce sont les enregistrements a remplir dans la table je l'ai deja posté ici et je crois que tu ma aidé aussi dans ce cas aussi

    il y a un cas ou je doit vidé la table et la remplir de nouveau a partir d'un fichier texte et un autre cas ou je doit la maitre ajour (tous les enregistrement), ajouté les nouveau enregistrements si elles n'existe pas ou les modifier si elle existe et diffère de celle du fichier texte

  19. #19
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 141
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 141
    Points : 41 308
    Points
    41 308
    Billets dans le blog
    63
    Par défaut
    c'est juste une boucle qui parcoure un fichier texte et lie les lignes car ce sont les enregistrements a remplir dans la table je l'ai deja posté ici et je crois que tu m'a aidé aussi dans ce cas
    je m'en souvenais pas

    toujours est-il que au lieu d'utiliser un Locate dans une table , je ferais une query du style
    Code QQuery : Sélectionner tout - Visualiser dans une fenêtre à part
     SELECT Key from Table2 Where Name=:N ANd ProviderID=:P AND KeyIndex=:K

    après avoir préparé la/les querys AVANT la Boucle

    cela donnerait ensuite

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    QQuery.ParamByName('N').asString:=System1;
    QQuery.ParamByName('P').asString:=Provider1;
    QQuery.ParamByName('K').asString:=Index1;
    QQuery.Active:=True;
    if QQuery.FieldByname('KEY').isNull then
      begin
        // Partie Insert
      end
    else if QQUery.FieldByname('KEY').asString<>key1 then
      begin
        // partie Update
      end;
    il serait certainement possible d'utiliser un ZUpdateSQL pour contenir les 3 querys et en faire quelque chose de plus condensé (plus de code SQL dans le texte, un seul prepare <-ça il faut que je vérifie)
    le tout devrait être plus rapide , en tout cas en réseau il n'y aurait pas photo

  20. #20
    Nouveau membre du Club
    Inscrit en
    Août 2007
    Messages
    159
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 159
    Points : 37
    Points
    37
    Par défaut
    Merci @SergioMaster

    j'ai déjà essayé avant la boucle puisque ça bien marche quand je vide la table et la remplir mais la dans ce cas ou je doit la maitre ajour j'ai eu des erreurs soit out of sequance ou param non trouver
    on tout les cas voici le code en entier (celui qui marche sans erreurs)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    procedure TSEditor.BitBtn2Click(Sender: TObject);
     var Softcam: textfile;
         Softcam_Line:string;
         System1,provider1,index1,key1:string;
         G,H,I:integer;
         nothex:Boolean;
    begin
    if not FileExists(Edit1.Text) then BitBtn10Click(nil)else
    if RadioButton1.Checked then
       begin
             BitBtn2.Enabled:=false;
        With ZConnection1 do
        begin
        ////vider la table pour un nouveau chargement
         Database :=(IncludeTrailingPathDelimiter(ExtractFileDir(GetModuleAbsolutePath))+'NW_keys.db');
         ExecuteDirect('DELETE from tblSystems');
        end;
     
    Ztable1.Refresh;
    Ztable2.Refresh;
     
    assignfile(Softcam,Edit1.Text);//,(IncludeTrailingPathDelimiter(ExtractFileDir(GetModuleAbsolutePath)))+'Softcam.key');
    reset(Softcam);
     
    ZQuery1.SQL.Text:='PRAGMA synchronous = OFF;';
    ZQuery1.ExecSQL;
     
    ZQuery1.SQL.Text:='INSERT INTO tblSystems(NAME,PROVIDERID,KEYINDEX,KEY,COMMENTS) VALUES (:pN,:pP,:pKI,:pK,:pC)';
    ZQuery1.Prepare;
     
     
    while not eof(Softcam) do
    begin
    nothex:=false;
     
       readln(Softcam,Softcam_Line);
     
        if (((uppercase(copy(Softcam_Line,1,1)))=('I')) or ((uppercase(copy(Softcam_Line,1,1))=('S'))) or
        ((uppercase(copy(Softcam_Line,1,1)))=('N')) or ((uppercase(copy(Softcam_Line,1,1)))=('W')) or
         ((uppercase(copy(Softcam_Line,1,1)))=('V')) or ((uppercase(copy(Softcam_Line,1,1)))=('F')))
        And ((copy(Softcam_Line,2,1))=(' '))
     
        then
            begin
                 I:=3;
                 while (copy(Softcam_Line,I,1)<>' ')and (copy(Softcam_Line,I,1)<>';')
                 and (I<= Length(Softcam_Line))and (not nothex)do
                 begin
                 if((uppercase(copy(Softcam_Line,I,1)) <>'0') and (uppercase(copy(Softcam_Line,I,1)) <>'1')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'2') and (uppercase(copy(Softcam_Line,I,1)) <>'3')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'4') and (uppercase(copy(Softcam_Line,I,1)) <>'5')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'6') and (uppercase(copy(Softcam_Line,I,1)) <>'7')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'8') and (uppercase(copy(Softcam_Line,I,1)) <>'9')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'A') and (uppercase(copy(Softcam_Line,I,1)) <>'B')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'C') and (uppercase(copy(Softcam_Line,I,1)) <>'D')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'E') and (uppercase(copy(Softcam_Line,I,1)) <>'F'))
                 then
                 begin
                 nothex:=true;
                 break;
                 end else
                 I:=I+1;
                 end;
                 H:=I+1;
                 while (copy(Softcam_Line,H,1)<>' ')and (copy(Softcam_Line,H,1)<>';')
                 and (H<= Length(Softcam_Line))and (not nothex) do
                 begin
                 H:=H+1;
                 end;
                 G:=H+1;
                 while (copy(Softcam_Line,G,1)<>' ')and (copy(Softcam_Line,G,1)<>';')
                 and (G<= Length(Softcam_Line))and (not nothex)do
                 begin
                 if((uppercase(copy(Softcam_Line,G,1)) <>'0') and (uppercase(copy(Softcam_Line,G,1)) <>'1')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'2') and (uppercase(copy(Softcam_Line,G,1)) <>'3')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'4') and (uppercase(copy(Softcam_Line,G,1)) <>'5')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'6') and (uppercase(copy(Softcam_Line,G,1)) <>'7')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'8') and (uppercase(copy(Softcam_Line,G,1)) <>'9')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'A') and (uppercase(copy(Softcam_Line,G,1)) <>'B')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'C') and (uppercase(copy(Softcam_Line,G,1)) <>'D')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'E') and (uppercase(copy(Softcam_Line,G,1)) <>'F'))
                 then
                 begin
                 nothex:=true;
                 break;
                 end else
                 G:=G+1;
                 end;
                 if nothex=false then
                 begin
                   System1:=uppercase(copy(Softcam_Line,1,1));
                   provider1:=uppercase(copy(Softcam_Line,3,I-3));
                   index1:=uppercase(copy(Softcam_Line,I+1,H-I-1));
                   key1:=uppercase(copy(Softcam_Line,H+1,G-H-1));
                end;
     
    if not Ztable2.locate('Name;ProviderID;KeyIndex',VarArrayOf([System1,provider1,index1]),[])
    then
    begin
                    // ajouter les données
    ZQuery1.Connection.StartTransaction;
    ZQuery1.ParamByName('pN').Value := System1;
    ZQuery1.ParamByName('pP').Value := provider1;
    ZQuery1.ParamByName('pKI').Value := index1;
    ZQuery1.ParamByName('pK').Value := key1;
    ZQuery1.ParamByName('pC').Value :=DateTimeToStr(now);
    ZQuery1.ExecSQL;
    end ;
    Ztable2.Refresh;
     
     
    /////////////////
    end ;
    end;
    Ztable1.Refresh;
    Ztable2.Refresh;
    MessageBox(Handle,'File Imported succefly ?', '',MB_OK ) ;
     
    closefile(Softcam);
    BitBtn2.Enabled:=true;
    //=====================================================================================
    //=====================================================================================
    end else
    if RadioButton2.Checked then
       begin
             BitBtn2.Enabled:=false;
     
    assignfile(Softcam,Edit1.Text);//,(IncludeTrailingPathDelimiter(ExtractFileDir(GetModuleAbsolutePath)))+'Softcam.key');
    reset(Softcam);
     
    ZQuery1.SQL.Text:='PRAGMA synchronous = OFF;';
    ZQuery1.ExecSQL;
     
     
    while not eof(Softcam) do
    begin
    nothex:=false;
     
       readln(Softcam,Softcam_Line);
     
        if (((uppercase(copy(Softcam_Line,1,1)))=('I')) or ((uppercase(copy(Softcam_Line,1,1))=('S'))) or
        ((uppercase(copy(Softcam_Line,1,1)))=('N')) or ((uppercase(copy(Softcam_Line,1,1)))=('W')) or
         ((uppercase(copy(Softcam_Line,1,1)))=('V')) or ((uppercase(copy(Softcam_Line,1,1)))=('F')))
        And ((copy(Softcam_Line,2,1))=(' '))
     
        then
            begin
                 I:=3;
                 while (copy(Softcam_Line,I,1)<>' ')and (copy(Softcam_Line,I,1)<>';')
                 and (I<= Length(Softcam_Line))and (not nothex)do
                 begin
                 if((uppercase(copy(Softcam_Line,I,1)) <>'0') and (uppercase(copy(Softcam_Line,I,1)) <>'1')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'2') and (uppercase(copy(Softcam_Line,I,1)) <>'3')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'4') and (uppercase(copy(Softcam_Line,I,1)) <>'5')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'6') and (uppercase(copy(Softcam_Line,I,1)) <>'7')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'8') and (uppercase(copy(Softcam_Line,I,1)) <>'9')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'A') and (uppercase(copy(Softcam_Line,I,1)) <>'B')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'C') and (uppercase(copy(Softcam_Line,I,1)) <>'D')
                 and (uppercase(copy(Softcam_Line,I,1)) <>'E') and (uppercase(copy(Softcam_Line,I,1)) <>'F'))
                 then
                 begin
                 nothex:=true;
                 break;
                 end else
                 I:=I+1;
                 end;
                 H:=I+1;
                 while (copy(Softcam_Line,H,1)<>' ')and (copy(Softcam_Line,H,1)<>';')
                 and (H<= Length(Softcam_Line))and (not nothex) do
                 begin
                 H:=H+1;
                 end;
                 G:=H+1;
                 while (copy(Softcam_Line,G,1)<>' ')and (copy(Softcam_Line,G,1)<>';')
                 and (G<= Length(Softcam_Line))and (not nothex)do
                 begin
                 if((uppercase(copy(Softcam_Line,G,1)) <>'0') and (uppercase(copy(Softcam_Line,G,1)) <>'1')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'2') and (uppercase(copy(Softcam_Line,G,1)) <>'3')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'4') and (uppercase(copy(Softcam_Line,G,1)) <>'5')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'6') and (uppercase(copy(Softcam_Line,G,1)) <>'7')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'8') and (uppercase(copy(Softcam_Line,G,1)) <>'9')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'A') and (uppercase(copy(Softcam_Line,G,1)) <>'B')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'C') and (uppercase(copy(Softcam_Line,G,1)) <>'D')
                 and (uppercase(copy(Softcam_Line,G,1)) <>'E') and (uppercase(copy(Softcam_Line,G,1)) <>'F'))
                 then
                 begin
                 nothex:=true;
                 break;
                 end else
                 G:=G+1;
                 end;
                 if nothex=false then
                 begin
                   System1:=uppercase(copy(Softcam_Line,1,1));
                   provider1:=uppercase(copy(Softcam_Line,3,I-3));
                   index1:=uppercase(copy(Softcam_Line,I+1,H-I-1));
                   key1:=uppercase(copy(Softcam_Line,H+1,G-H-1));
                end;
    if (Ztable2.locate('Name;ProviderID;KeyIndex',VarArrayOf([System1,provider1,index1]),[]))
    and
    (Ztable2.FieldByName('Key').AsString <> key1)
    then
    begin
    ZQuery1.close;
    ZQuery1.sql.clear;
    ZQuery1.SQL.Add('UPDATE tblSystems');
    ZQuery1.SQL.Add('SET KEY = :key1');
    ZQuery1.SQL.Add('WHERE Name = :System1 AND ProviderID = :provider1 AND KeyIndex = :index1');
    ZQuery1.Prepare;
    ZConnection1.StartTransaction;
    ZQuery1.paramByName('Key1').AsString :=Key1 ;
    ZQuery1.paramByName('System1').AsString :=System1 ;
    ZQuery1.paramByName('Provider1').AsString :=Provider1 ;
    ZQuery1.paramByName('index1').AsString :=index1;
    ZQuery1.ExecSQL;
    end else
    ZQuery1.SQL.Text:='INSERT INTO tblSystems(NAME,PROVIDERID,KEYINDEX,KEY,COMMENTS) VALUES (:pN,:pP,:pKI,:pK,:pC)';
    ZQuery1.Prepare;
    if not Ztable2.locate('Name;ProviderID;KeyIndex',VarArrayOf([System1,provider1,index1]),[])
    then
    begin
    ZConnection1.StartTransaction;
                    // ajouter les données
    ZQuery1.Connection.StartTransaction;
    ZQuery1.ParamByName('pN').AsString := System1;
    ZQuery1.ParamByName('pP').AsString := provider1;
    ZQuery1.ParamByName('pKI').AsString := index1;
    ZQuery1.ParamByName('pK').AsString := key1;
    ZQuery1.ParamByName('pC').AsString :=DateTimeToStr(now);
    ZQuery1.ExecSQL;
    end ;
    Ztable2.Refresh;
    /////////////////
    end ;
    end;
    Ztable1.Refresh;
    Ztable2.Refresh;
    MessageBox(Handle,'File Imported succefly ?', '',MB_OK ) ;
     
    closefile(Softcam);
    BitBtn2.Enabled:=true;
    end;
    end;

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Impossible d'ajouter des enregistrements via une requête
    Par roman33 dans le forum Requêtes et SQL.
    Réponses: 4
    Dernier message: 05/06/2009, 21h20
  2. Modifier ou supprimer un enregistrement par une requête SQL
    Par afatdz dans le forum Bases de données
    Réponses: 6
    Dernier message: 29/09/2008, 09h01
  3. [ACCESS] Ajouter un enregistrement dans une requête multi-table
    Par access_balou dans le forum Requêtes et SQL.
    Réponses: 4
    Dernier message: 23/09/2008, 14h43
  4. Enlever l'unicité par une requète SQL
    Par philnext dans le forum Langage SQL
    Réponses: 6
    Dernier message: 25/06/2007, 14h22
  5. Réponses: 1
    Dernier message: 04/06/2007, 10h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo