Delphi XE4 sample (Step by Step how to download data from CSV to SQL Table)

1.  Create Table in SQL database as fallows.
           CREATE TABLE SAMPLE 
                            ( STAFF_ID VARCHAR(30),
                               DATEX VARCHAR(50),
                               TIMEX VARCHAR(50),
                               STATUS VARCHAR(50) )

2. Add database to your form
3. Create connection to data base
4. Add Query ti table and rename as QR1.
5. Create Folder in C:\DataCSV.
6. Create new text file in C:\DataCSV and rename as "Data".
7. Copy below data to text file.

161|9/7/2013|16:59|0
161|9/7/2013|16:59|0
161|9/7/2013|16:59|1
161|9/7/2013|16:59|1
161|9/7/2013|17:0|1
161|9/7/2013|17:0|1
161|9/7/2013|17:0|0
161|9/7/2013|17:0|1
161|9/7/2013|17:0|0


8.  Open new delphi  VCL application.
9.  Rename the form as "frmDownload".
10.  Add new button and rename as "Download".
11.  Copy and past below code to the button as click procedure.

procedure TForm1.DownloadClick(Sender: TObject);
var
      strlst  : Tstringlist;
      myfile  : TextFile;
      search  : string;
      i,j  : integer;
      bb,aa:string;

  temp, fName, sName, eMail, xx: string;
  sgItem: TStringList;
  f: textfile;
begin

assignfile(f, 'C:\DataCSV\Data.txt');
  reset(f);
  sgItem := TStringList.Create;
  StringGrid1.RowCount := 2;
  while not eof(f) do
  begin
    readln(f, temp);
    fName := copy(temp, 1, pos('|', temp) - 1);
    delete(temp, 1, pos('|', temp));
    sName := copy(temp, 1, pos('|', temp) - 1);
    delete(temp, 1, pos('|', temp));
    eMail := copy(temp, 1, pos('|', temp) - 1);
    delete(temp, 1, pos('|', temp));

 

  //GET REC NO start--
  aa:= 'select * from SAMPLE' ;
  with QR1 do
    begin
      Active:=False;
      SQL.Clear;
      SQL.Add(aa);
      ExecSQL;
    end;
    data.QR1 ;
    bb:=inttostr((data.Query1.RecordCount)+1);
   // showmessage(bb);
  //GET REC NO Finish


  //Add to Table start
  SQLAdd:=('INSERT INTO SAMPLE ('+
        'REC_ID,'+
        'STAFF_ID,'+
        'DATEX,'+
        'TIMEX,'+
        'STATUS,'+
        'FUPDATE'+
        ')VALUES '+
        '('''+bb+''''+
        ','''+fName+''''+
        ','''+sName+''''+
        ','''+eMail+''''+
        ','''+xx+''''+
        ','''+'0'+''')'+
        ';');

 with data.QR1 do
  begin
    Active:=False;
    SQL.Clear;
    SQL.Add(SQLAdd);
    ExecSQL;

  end;
  recal:= 'select * from SAMPLE ' ;
  with data.QR1 do

  begin

    Active:=False;
    SQL.Clear;
    SQL.Add(recal);
    ExecSQL;

  end;
 //Add to Table finish
  end;
  sgItem.Free;
  closefile(f);

  AssignFile(F, 'C:\DataCSV\Data.txt');
  Rewrite(F);
  CloseFile(F);


end;

12. Run your application.

Comments

Popular posts from this blog

Delphi stringgrid Sample (Using for loop)

How to save jpg image to database and then load it in Delphi using FIBplus and TImage?

Object Oriented Programming in Delphi A Guide for Beginners