Monday, August 03, 2020

Cara Input Data ke Database

Tags

Cara Input Data ke Database - Input Record atau memasukan data kedalam suatu database memang sangat mudah dilakukan. Telebih sekarang banyak tools pendukung untuk melakukan insert record data to database.

Pertama > Untuk Lokal Komputer (maksudnya data yang kan kita insert berada dikomputer kita...hehheheh) dan
Kedua > Untuk Server Pusat (jika kita tidak punya data pada komputer kita, kita bisa ambil data yang telah di Insert di pusat database dan dipindahkan ke database server lokal via jaringan intenet..hehehe)

Saya disini hanya sedikit sharing aja mungkin berguna bagi Mas Bro / Sis . hanya saja saat dijalankan agak sedikit memerlukan waktu dan code ini sudah saya pakai di program saya. dan hasilnya memuaskan walaupun sedikit lama. dikarenakan tiap kali insert data saya > 5.000 record atau lebih dan > 100.000 data tiap bulannya, berikut codenya. Untuk Koneksinya memakai Zeos, dan hanya mendukung untuk File (DBF) untuk yang Local Computer Untuk File Selain DBF masih dalam pengembangan (file Excel, csv, txt).

Cara Input Data ke Database

Source Code Delphi 7 Input Banyak Data
unit Unit29;
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, sGauge, StdCtrls, Buttons, sBitBtn, sComboBox, sLabel,

  ZAbstractTable, ZDataset, DB, ZAbstractRODataset, ZAbstractDataset,

  sRadioButton, sGroupBox, sEdit, sDialogs, DBTables, sSkinManager,

  sSkinProvider;

type

  TForm29 = class(TForm)

    ZDataBBSPusat: TZQuery;

    ZTabDataBBSLocal: TZTable;

    sGroupBox1: TsGroupBox;

    sLabel1: TsLabel;

    sLabel2: TsLabel;

    sLabel3: TsLabel;

    sComboBox3: TsComboBox;

    sComboBox2: TsComboBox;

    sComboBox1: TsComboBox;

    sLabel4: TsLabel;

    sLabel5: TsLabel;

    sLabel6: TsLabel;

    sBitBtn1: TsBitBtn;

    sGauge1: TsGauge;

    sGroupBox2: TsGroupBox;

    sRadioButton1: TsRadioButton;

    sRadioButton2: TsRadioButton;

    sGroupBox3: TsGroupBox;

    sEdit1: TsEdit;

    sBitBtn2: TsBitBtn;

    sGauge2: TsGauge;

    sLabel7: TsLabel;

    sLabel8: TsLabel;

    sOpenDialog1: TsOpenDialog;

    Table1: TTable;

    sBitBtn3: TsBitBtn;

    sSkinProvider1: TsSkinProvider;

    procedure FormCreate(Sender: TObject);

    procedure sComboBox2Change(Sender: TObject);

    procedure sComboBox3Change(Sender: TObject);

    procedure sBitBtn1Click(Sender: TObject);

    procedure sBitBtn2Click(Sender: TObject);

    procedure sBitBtn3Click(Sender: TObject);

    procedure sRadioButton1Click(Sender: TObject);

    procedure sRadioButton2Click(Sender: TObject);

    procedure sComboBox3Click(Sender: TObject);

    procedure FormClose(Sender: TObject; var Action: TCloseAction);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



var

  Form29: TForm29;



implementation



uses TypInfo, Unit1;



{$R *.dfm}



procedure TForm29.FormCreate(Sender: TObject);

begin

sLabel4.Hide;

sGauge1.Progress := 0;

sGauge2.Progress := 0;

sBitBtn1.Enabled := False;

sGroupBox1.Hide;

sGroupBox3.Hide;

sBitBtn3.Enabled := False;

end;



procedure TForm29.sComboBox2Change(Sender: TObject);

begin

if sComboBox2.Text ='JANUARI' THEN

BEGIN

  sLabel4.Caption :='01';

END

ELSE if sComboBox2.Text ='FEBRUARI' THEN

BEGIN

  sLabel4.Caption :='02';

END

ELSE if sComboBox2.Text ='MARET' THEN

BEGIN

  sLabel4.Caption :='03';

END

ELSE if sComboBox2.Text ='APRIL' THEN

BEGIN

  sLabel4.Caption :='04';

END

ELSE if sComboBox2.Text ='MEI' THEN

BEGIN

  sLabel4.Caption :='05';

END

ELSE if sComboBox2.Text ='JUNI' THEN

BEGIN

  sLabel4.Caption :='06';

END

ELSE if sComboBox2.Text ='JULI' THEN

BEGIN

  sLabel4.Caption :='07';

END

ELSE if sComboBox2.Text ='AGUSTUS' THEN

BEGIN

  sLabel4.Caption :='08';

END

ELSE if sComboBox2.Text ='SEPTEMBER' THEN

BEGIN

  sLabel4.Caption :='09';

END

ELSE if sComboBox2.Text ='OKTOBER' THEN

BEGIN

  sLabel4.Caption :='10';

END

ELSE if sComboBox2.Text ='NOPEMBER' THEN

BEGIN

  sLabel4.Caption :='11';

END

ELSE if sComboBox2.Text ='DESEMBER' THEN

BEGIN

  sLabel4.Caption :='12';

END;

end;



procedure TForm29.sComboBox3Change(Sender: TObject);

var jum : Integer; pesan : String;

begin

pesan := 'Database  Belum Diaploud di Database Pusat';

with ZDataBBSPusat do

  begin

    Close;

    SQL.Clear;

    SQL.Add('SELECT * FROM databbs WHERE A = '+sComboBox1.Text+' AND B = ' +QuotedStr(sLabel4.Caption)+' AND C = ' +sComboBox3.Text);

    Open;

    if ZDataBBSPusat.RecordCount = 0 then

    begin

      Application.MessageBox(PChar(pesan),'Informasi',MB_OK or MB_ICONINFORMATION);

    end

    else

    begin

      jum := ZDataBBSPusat.RecordCount;

      sLabel6.Caption := IntToStr(jum);

      sBitBtn1.Enabled := True;

      Close;

    end;

    //sGauge1.MaxValue := ZDataBBSPusat.RecordCount ;

    //Application.ProcessMessages;

  end;

end;



procedure TForm29.sBitBtn1Click(Sender: TObject);

VAR pesan : String;

begin

pesan := 'Database Telah Berhasil Di Download Dari Pusat';

ZTabDataBBSLocal.TableName := 'databbs';

ZTabDataBBSLocal.Open;

with ZDataBBSPusat do

  begin

    //Close;

    SQL.Clear;

    SQL.Add('SELECT * FROM databbs WHERE BC = '+sComboBox1.Text+' AND IMONTH = ' +sLabel4.Caption+' AND IYEAR = ' +sComboBox3.Text);

    Open;

    WHILE NOT ZDataBBSPusat.Eof DO

      BEGIN

        WITH  ZTabDataBBSLocal do

          begin

            Append;

            FIELDBYNAME('1').AsString := ZDataBBSPusat.FIELDBYNAME('a').AsString;

            FIELDBYNAME(2').AsString := ZDataBBSPusat.FIELDBYNAME(b').AsString;

            FIELDBYNAME('3').AsString := ZDataBBSPusat.FIELDBYNAME('c').AsString;

            FIELDBYNAME('4').AsString := ZDataBBSPusat.FIELDBYNAME('d').AsString;

            ------------- (maaf saya potong karena untuk menyingkat halaman karena hampir sama untuk codenya)


            -------------


            FIELDBYNAME('14').AsString := ZDataBBSPusat.FIELDBYNAME('n').AsString;


            Post;

            ZDataBBSPusat.Next;
          end;

        sGauge1.Progress := sGauge1.Progress + 1;

      END;

      if sGauge1.Progress = 100 then

      begin

        Application.MessageBox(pchar(pesan),'Informasi',MB_OK or MB_ICONINFORMATION);

        sComboBox1.Text := '';

        sComboBox2.Text := '';

        sComboBox3.Text := '';

        sLabel6.Caption := '0';

        sGauge1.Progress := 0;

        sBitBtn1.Enabled := False;

      end;

  end;

end;



procedure TForm29.sBitBtn2Click(Sender: TObject);

var jml : integer;

begin

sGauge2.MinValue := 0;

if sOpenDialog1.Execute then

  begin

    with Table1 do

      begin

        Close;

        TableName := sOpenDialog1.FileName;

        Open;

        sEdit1.Text := TableName;

        jml := Table1.RecordCount;

        sLabel8.Caption := FormatFloat('#,#',jml);

        sBitBtn3.Enabled := True;

      end;

  end;

end;



procedure TForm29.sBitBtn3Click(Sender: TObject);

begin

sGauge2.MaxValue := Table1.RecordCount ;

Application.ProcessMessages;

ZTabDataBBSLocal.TableName := 'databbs';

ZTabDataBBSLocal.Open;

WHILE NOT Table1.Eof DO


  BEGIN


    WITH ZTabDataBBSLocal DO


      BEGIN

        Append; 
            FIELDBYNAME('1').AsString := ZDataBBSPusat.FIELDBYNAME('a').AsString;

            FIELDBYNAME(2').AsString := ZDataBBSPusat.FIELDBYNAME(b').AsString;

            FIELDBYNAME('3').AsString := ZDataBBSPusat.FIELDBYNAME('c').AsString;

            FIELDBYNAME('4').AsString := ZDataBBSPusat.FIELDBYNAME('d').AsString;

            ------------- (maaf saya potong karena untuk menyingkat halaman karena hampir sama untuk codenya)


            -------------


            FIELDBYNAME('14').AsString := ZDataBBSPusat.FIELDBYNAME('n').AsString;


            Post;

                Table1.Next;
        //end;

      end;

      sGauge2.Progress := sGauge2.Progress + 1;

  end;



if sGauge2.Progress = Table1.RecordCount then

  begin

    Application.MessageBox('Data Telah Selesai diDownload'+chr(10)+'Barcode Pengambilan dan Pelaporan Bisa Dilakukan','Informasi',MB_OK or MB_ICONINFORMATION);
    sGauge2.Progress := 0;
    sBitBtn3.Enabled := False;
    sEdit1.Text :='';
    sLabel8.Caption := '0';
  end;
end;

procedure TForm29.sRadioButton1Click(Sender: TObject);
begin
if sRadioButton1.Checked = True then
  Begin
    sGroupBox3.Show;
    sEdit1.Text :='';
    sBitBtn3.Enabled := False;
    sLabel8.Caption :='0';
    sLabel6.Caption :='0';
    sComboBox1.Text :='';
    sComboBox2.Text :='';
    sComboBox3.Text :='';
    sBitBtn1.Enabled := False;
    sRadioButton2.Checked := False;
    sGroupBox1.Hide;
  end
else
  begin
    sGroupBox3.Hide;
  end;
end;

procedure TForm29.sRadioButton2Click(Sender: TObject);
begin
if sRadioButton2.Checked = True then
  Begin
    sGroupBox1.Show;
    sComboBox1.Text :='';
    sComboBox2.Text :='';
    sComboBox3.Text :='';
    sBitBtn1.Enabled := False;
    sEdit1.Text :='';
    sBitBtn3.Enabled := False;
    sLabel8.Caption :='0';
    sLabel6.Caption :='0';
    sRadioButton1.Checked := False;
    sGroupBox3.Hide;
  end
else
  begin
    sGroupBox1.Hide;
  end;
end;

procedure TForm29.sComboBox3Click(Sender: TObject);
begin
sLabel6.Caption := '0';
sBitBtn1.Enabled := False;
end;

procedure TForm29.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
Form1.DownloadDatabase1.Enabled := True;
end;

end.

Hasil nya jika Dirun
input record banyak


Maaf jika ejaan tulisan saya membuat sobat sedikit bingung. Sekian Code dari saya untuk lebih jelasnya silahkan di coba, dan jika masih terjadi error bisa kirim komentar. Insakallah akan saya tanggapi jika ada waktu.

Demikian cara input data ke database mysql dengan delphi semoga dapat bermanfaat bagi kita semua.

Silahkan berkomentar dengan baik, asal tidak melanggar peraturan dibawah ini.
1. NO SPAM
2. NO SARA
3. NO LINK AKTIVE

Terimakasih
EmoticonEmoticon