Q&A

  • 엑셀파일을 받아서 insert시 query was empty 오류가 나는데 도와주세요..
mysql DB에 Insert 시킬려고 하는데 자꾸
Query was empty 오류가 생기네요.
ㅜ_ㅜ;
test 테이블 DB 구조는

no        int(11) , auto_increment
line      varchar(5)
demandid  varchar(10)
bom       varchar(10)
model     varhcar(50)
lot       int(11)
qty       int(11)
date      date
pn1       varchar(20)
pn2       varchar(20)
pn3       varchar(20)
pn4       varchar(20)
pn5       varchar(20)

<!--CodeS-->
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Db, ADODB, Grids, DBGrids, URGrids, URDBGrid, URLabels,
  ExtCtrls, URMGrid, ComCtrls, ZAbstractRODataset, ZAbstractDataset,
  ZDataset, ZConnection, ZAbstractTable;

type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    DataSource1: TDataSource;
    Button2: TButton;
    ListBox1: TListBox;
    Panel1: TPanel;
    wLabel1: TwLabel;
    RealGrid1: TRealGrid;
    Panel2: TPanel;
    Button3: TButton;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    DataSource2: TDataSource;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure MainColumnTitleClick(AColumn: TwColumn);
    procedure Button3Click(Sender: TObject);
    procedure InsertData;

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

const
  // Grid Columns Index
  GR_line = 0;
  GR_demandid = 1;
  GR_bom = 2;
  GR_model = 3;
  GR_lot = 4;
  GR_qty = 5;
  GR_date = 6;
  GR_pn1 = 7;
  GR_pn2 = 8;
  GR_pn3 = 9;
  GR_pn4 = 10;
  GR_pn5 = 11;

SQL_INSERT_DATA = 'INSERT INTO test                                    '+#13#10+
                  ' (line, demandid, bom,model,lot,qty,date,           '+#13#10+
                  ' pn1,pn2,pn3,pn4,pn5)                               '+#13#10+
                  ' VALUES                                             '+#13#10+
                  ' (:line, :demandid, :bom, :model, :lot, :qty,:date, '+#13#10+
                  ' :pn1, :pn2, :pn3, :pn4, :pn5)                      ';



implementation

{$R *.DFM}

procedure TForm1.InsertData;
  var
  nRow,i: Integer;

begin

  with ZQuery1, RealGrid1 do
  begin
    // dbMain.StartTransaction;
    // RDBMS 사용시 주석을 지워 주세요, 트랜잭션 처리 방법
    try
      for nRow := 1 to RowCount -1 do
      begin
        if RowState[nRow] = wrInserted then
          SQL.Text := SQL_INSERT_DATA;
        begin
          ParamByName('line').AsString  := Cells[GR_line, nRow].AsString;
          ParamByName('demandid').AsString := Cells[GR_demandid, nRow].AsString;
          ParamByName('bom').AsString := Cells[GR_bom, nRow].AsString;
          ParamByName('model').AsString := Cells[GR_model, nRow].AsString;
          ParamByName('lot').AsInteger := Cells[GR_lot, nRow].AsInteger;
          ParamByName('qty').AsInteger := Cells[GR_qty, nRow].AsInteger;
          ParamByName('date').AsString := Cells[GR_date, nRow].AsString;
          ParamByName('pn1').AsString := Cells[GR_pn1, nRow].AsString;
          ParamByName('pn2').AsString := Cells[GR_pn2, nRow].AsString;
          ParamByName('pn3').AsString := Cells[GR_pn3, nRow].AsString;
          ParamByName('pn4').AsString := Cells[GR_pn4, nRow].AsString;
          ParamByName('pn5').AsString := Cells[GR_pn5, nRow].AsString;
//         showmessage(SQL.Text);

          for i:=0 to ZQuery1.Params.Count-1 do
          begin
//           Memo1.Lines.Add(SQL.Text);
          end;

          ExecSQL;

         end;

      end;
      //dbMain.Commit;
      for nRow := 0 to RowCount -1 do
        RowState[nRow] := wrNormal;
      Refresh;
    except
      on E: Exception do
      begin
        //dbMain.Rollback;
        ShowMessage(E.Message + ' [저장오류]');
      end;
    end;
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   if not OpenDialog1.Execute then Exit;
   with ADOConnection1 do
   begin
      Connected := False;
      ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;' +
                          'Data Source=' + OpenDialog1.FileName + ';Mode=Share Deny None;Extended Properties=Excel 8.0';
      Connected := True;

      GetTableNames(ListBox1.Items);
      if ListBox1.Items.Count > 0 then ListBox1.ItemIndex := 0;
   end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   with ADOTable1,RealGrid1 do
   begin
      Close;
      TableName := '[' + ListBox1.Items[ListBox1.ItemIndex] + ']';
      Open;


      while not EOF do
      begin
      AddRow;

        Cells[GR_line   , RowCount - 1].AsString := FieldByName('Line').AsString;
        Cells[GR_demandid   , RowCount - 1].AsString := FieldByName('Demand Id').AsString;
        Cells[GR_bom   , RowCount - 1].AsString := FieldByName('BOM').AsString;
        Cells[GR_model   , RowCount - 1].AsString := FieldByName('모델').AsString;
        Cells[GR_lot   , RowCount - 1].AsInteger := FieldByName('LOT').AsInteger;
        Cells[GR_qty   , RowCount - 1].AsInteger := FieldByName('잔량').AsInteger;
        Cells[GR_date   , RowCount - 1].AsString := FieldByName('시작일').AsString;
        Cells[GR_pn1   , RowCount - 1].AsString := FieldByName('1').AsString;
        Cells[GR_pn2   , RowCount - 1].AsString := FieldByName('2').AsString;
        Cells[GR_pn3   , RowCount - 1].AsString := FieldByName('3').AsString;
        Cells[GR_pn4   , RowCount - 1].AsString := FieldByName('4').AsString;
        Cells[GR_pn5   , RowCount - 1].AsString := FieldByName('5').AsString;
        Next;
      end;
     ShowMessage('엑셀파일을 읽기 완료');
   end;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Action := caFree;

end;

procedure TForm1.MainColumnTitleClick(AColumn: TwColumn);
begin
   if AColumn.Title.SortMark = wsmAsc then
    RealGrid1.Sort(AColumn.Index, True)
  else
    RealGrid1.Sort(AColumn.Index);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
   InsertData;
end;


end.
<!--CodeE-->
1  COMMENTS
  • Profile
    최용일 2005.03.17 22:13
    안녕하세요. 최용일입니다.

    Button3을 클릭햇을때 DB에 추가되는거 같은데....

    InsertData함수의 for문에서


    nRow = 1일때 위 if문이 False로되면 SQL.Text는 비어있게 되는데... (rue가 되면야 상관없을듯하지만...)

    파라매터 넣고 ExecSQL;호출하면 에러가 나는게 당연할듯합니다.

    ^^ 항상 즐코하세요...