Sign in Join
Wiki copytemporary

TSQLScript/fr

English · Français · 日本語

TSQLScript vous laisse passer un ensemble de commandes SQL en une seule exécution. C'est utile si vous voulez mettre en place une nouvelle base de données ou mettre à jour une base existante.

TSQLScript est disponible pour FPC et Lazarus et marche pour toute base de données que SQLdb prend en charge. Il peut être trouvé dans l'onglet SQLdb de la palette de composants.

Exemple

Supposez que vous avez un ensemble de commandes SQL telles que la sortie SQL suivante d'un script pour Firebird dans l'outil FlameRobin (dans Properties/DDL) :

Remarquez qu'au contraire de TSQLQuery, TSQLScript nécessite un point-virgule après chaque ordre SQL.

Cet exemple suppose que vous avez des objets connexion SQL et transaction définis.

pascal
uses
...sqldb, TIBConnection,...
const
  DBSchemaFile='dbreporter.sql';
var
  FBScript:TSQLScript;
  ScriptText:TStringList;
  TranWasStarted: boolean;
begin
  TranWasStarted:=FTran.Active; //Ftran is the transaction, defined somewhere in our class
  if not TranWasStarted then FTran.StartTransaction;
  FBScript:=TSQLScript.Create(nil);
  ScriptText:=TStringList.Create;
  try
    if not fileexists(DBSchemaFile) then 
      raise Exception.CreateFmt('dbreporter: could not load database schema file %s',[DBSchemaFile]);
    ScriptText.LoadFromFile(DBSchemaFile);
    FBScript.DataBase:=(FConn as TIBConnection);
    FBScript.Transaction:=FTran;
    FBScript.Script:=ScriptText;
    // Now everything is loaded in, run all commands at once:
    FBScript.Execute;
    //... and then commit to make them stick and show them to the SQL that comes
    // after the commit
    FTran.Commit;
  finally
    FBScript.Free;
    ScriptText.Free;
  end;
  // Make sure we leave the transaction state as we found it, handy for switchnig
  // between explicit start/commit transaction and commitretaining:
  if TranWasStarted then FTran.StartTransaction;

Voir aussi \examples\database\tsqlscript dans votre dossier Lazarus pour un exemple de programme faisant la démonstration de TSQLScript.

Warning: (au moins) FPC 2.6.4 et antérieur : TSQLScript n'analysera pas tous les ordres DDL de Firebird, vous aurez donc besoin de tester votre script en avance. TSQLScript a été amélioré dans le tronc FPC (2.7.1). Une alternative serait d'appeler l'exécutable ISQL de Firebird en utilisant par exemple TProcess.

Note: Définir la propriété CommentsInSQL à 'False' peut aider à améliorer la fiabilité de TSQLScript si, p.ex., vous avez des commentaires dans des déclarations de procédures stockées.


From the Free Pascal wiki, page "TSQLScript/fr", as it stood on 2020-03-02 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.