Import csv

Résolu
quentind59 Messages postés 11 Date d'inscription jeudi 8 mars 2007 Statut Membre Dernière intervention 25 novembre 2008 - 4 nov. 2008 à 15:04
quentind59 Messages postés 11 Date d'inscription jeudi 8 mars 2007 Statut Membre Dernière intervention 25 novembre 2008 - 25 nov. 2008 à 11:11
Bonjour,  je dois programmer sous delphi l'import d'un fichier csv dans une base de données interbase.
Mais mon code plante!! :s

 IBquery1.Active := False;
 IBquery1.SQL.Clear;
 IBquery1.SQL.Add('LOAD DATA INFILE ''''C:\quantum\orders.txt'''' REPLACE INTO TABLE orders');
 IBquery1.SQL.Add('FIELDS TERMINATED BY '''';'''' ENCLOSED BY ''''".''''"''''."'''' ESCAPED BY ''''\\\\'''' ');
 IBquery1.SQL.Add('LINES TERMINATED BY ''''\\r\\n''''');
 IBquery1.Active := True;

Quelqu'un voit la solution ou a peut être une autre solution?

Merci d'avance.
A voir également:

16 réponses

cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 14
4 nov. 2008 à 19:49
Voilà une solution générale qui fonctionne sur mon micro avec mes composants (avec licence)

A toi d'adapter le code avec tes composants en précisant également qu'il n'y a pas de clé primaire auto-incrémentale, mais que si tu en as une ça marchera aussi, en modifiant l'index j :

unit Unit1;


interface


uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, DBTables, StrUtils, IBODataset, IB_Components;


type
  TForm1 = class(TForm)
    Button1: TButton;
    IBOQuery1: TIBOQuery;
    Connexion: TIB_Connection;
    Transaction: TIB_Transaction;
    IBOQuery1CODETEMPO: TIntegerField;
    IBOQuery1CHAMP1: TDateField;
    IBOQuery1CHAMP2: TStringField;
    IBOQuery1CHAMP3: TIntegerField;
    procedure Button1Click(Sender: TObject);
    procedure ConnexionBeforeConnect(Sender: TIB_Connection);
    procedure FormCreate(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;


var
  Form1: TForm1;


implementation


{$R *.dfm}
   ///////////pm : f0xi   ////////////////////////////
function Parser(const Chaine, Separat: string; const Occurence: cardinal = 1): string;
var
  i, p1, p2, ld: integer;
begin
  Result := '';
  if (posex(Separat, Chaine[1]) = 1) and (Occurence <= 1) then exit;
  p1 := 1;
  ld := Length(Separat);
  for I := 1 to Occurence - 1 do
  begin
    p1 := posex(Separat, Chaine, p1);
    if P1 = 0 then
      exit
    else
      p1 := p1 + ld;
  end;
  p2 := posex(Separat, Chaine, p1);
  if p2 = 0 then
    p2 := length(Chaine) + 1;
  Result := copy(Chaine, p1, p2 - p1);
end;



procedure TForm1.ConnexionBeforeConnect(Sender: TIB_Connection);
begin
  Connexion.Username : = 'login';
  Connexion.Password := 'password';
end;



procedure TForm1.Button1Click(Sender: TObject);
var
  SL: TStringList;
  i, j: integer;
begin
  try
    screen.Cursor : = CrHourGlass;
    SL := TStringList.create;
    SL.loadFromFile('FichCSV.csv');   // ton fichier .CSV


    for i := 0 to SL.Count - 1 do
    begin
      IBOQuery1.Append;
      for j := 0 to IBOQuery1.FieldCount - 1 do
        IBOQuery1.fields[ j ].Text : = Parser(SL.Strings[i], ';', j + 1);
      IBOQuery1.Post;
    end;



  finally
    screen.Cursor := CrDefault;
    SL.Free;
  end;
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
  IboQuery1.Open;
end;



end.


cantador
3