Topic: [Script] Import CSV file into the database
Import CSV file into the database
procedure Form1_Button2_OnClick (Sender: string; var Cancel: boolean);
var
OpenDialog: TOpenDialog;
sl: TStringList;
arrStr: array of string;
i,c: integer;
begin
OpenDialog := TOpenDialog.Create(Form1);
OpenDialog.InitialDir := ExtractFileDir(Application.Exename);
if OpenDialog.Execute then
begin
sl := TStringList.Create;
sl.LoadFromFile (OpenDialog.FileName);
c := sl.Count - 1;
for i := 0 to c do
begin
arrStr := SplitString(sl[i], ';');
// LastName
if arrStr[0] <> '' then arrStr[0] := ''''+ ReplaceStr(arrStr[0], '''', '''''') + ''''
else arrStr[0] := 'NULL';
// FirstName
if arrStr[1] <> '' then arrStr[1] := ''''+ ReplaceStr(arrStr[1], '''', '''''') + ''''
else arrStr[1] := 'NULL';
SQLExecute ('INSERT INTO base (lastname, firstname) VALUES ('+ arrStr[0] +','+ arrStr[1]+');');
Form1.Label1.Caption := IntToStr(i+1) + ' of ' + IntToStr(c+1);
Application.ProcessMessages;
end;
sl.Free;
Form1.TableGrid1.dbUpdate;
end;
OpenDialog.Free;
end;
begin
end.
Download project:
http://myvisualdatabase.com/forum/misc. … download=1