제가 텍스트화일을 읽어와서 콤마를 구분자로하여 db에 저장하는 것을 하려고하느데요
자꾸 bde에러가 나네여...누가좀 어디가 어떻게 잘못됐는지좀 알려주세요.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls, ActnList, Db, DBTables, Grids, DBGrids;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
f1: TMenuItem;
o1: TMenuItem;
x1: TMenuItem;
OD: TOpenDialog;
ActionList1: TActionList;
Open: TAction;
exit: TAction;
SD: TSaveDialog;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Query1: TQuery;
procedure OpenExecute(Sender: TObject);
procedure exitExecute(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.OpenExecute(Sender: TObject);
Var
I,K,Cnt,R_SEQ : Integer;
Num, kNo, kMemo, Name, Dept, Day : string;
FP : Textfile;
Buf : String;
begin
Cnt := 0;
if OD.Execute then
begin
AssignFile(FP,OD.FileName);
Reset(FP);
While not Eof(FP) do
Begin
Num :=''; kNo :=''; Buf :=''; kMemo :='';Name:='';
Dept :=''; Day :='';
Readln(FP,Buf);
K:=0;
for I:=1 to Length(buf) do
begin
if buf[I] = ',' then
K := K + 1
else
case K of
0 : Num := Num + Buf[i];
1 : kNo := kNo + Buf[i];
2 : kMemo := kMemo + buf[i];
3 : Name := Name + buf[i];
4 : Dept := Dept + buf[i];
5 : Day := Day + buf[i];
end;
kNo := Trim(kNo);
kMemo := Trim(kMemo);
Name := Trim(Name);
Dept := Trim(Dept);
Day := Trim(Day);
with Query1 do
begin
SQL.Clear;
SQL.Add('insert into test');
SQL.Add('values(:r1,:r2,:r3,:r4,:r5)');
{ SQL.Add('update test');
SQL.Add('Number =:r1,');
SQL.Add('kMemo =:r2,');
SQL.Add('Name =:r3,');
SQL.Add('Dept =:r4,');
SQL.Add('Day =:r5,');}
Query1.ParambyName('r1').AsString := kNo;
Query1.ParambyName('r2').AsString := kMemo;
Query1.ParambyName('r3').AsString := Name;
Query1.ParambyName('r4').AsString := Dept;
Query1.ParambyName('r5').AsString := Day;
ExecSQL;
end;
end;
end;
end;
end;
procedure TForm1.exitExecute(Sender: TObject);
begin
close;
end;
end.
먼저 Insert문이 있구 다음에 Update를 넣은 다음 Query를 실행시켰군요.
그렇게 하지시 말구...
Sql.Clear;
Insert문 삽입
파라미터 대입
Query 실행
Sql.Clear;
Update문 삽입
파라미터 대입
Query 실행
이렇게 하시면 됩니다.
정 이 방법이 싫으시다면 두개의 Query컴포넌트를 사용해서 연속적으로 실행시키거나, 아니면 Stored Procedure를 사용하세요.
제 개인적으로는 Stored Procedure를 추천합니다.
????????? wrote:
> 제가 텍스트화일을 읽어와서 콤마를 구분자로하여 db에 저장하는 것을 하려고하느데요
> 자꾸 bde에러가 나네여...누가좀 어디가 어떻게 잘못됐는지좀 알려주세요.
>
> unit Unit1;
>
> interface
>
> uses
> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
> Menus, StdCtrls, ActnList, Db, DBTables, Grids, DBGrids;
>
> type
> TForm1 = class(TForm)
> MainMenu1: TMainMenu;
> f1: TMenuItem;
> o1: TMenuItem;
> x1: TMenuItem;
> OD: TOpenDialog;
> ActionList1: TActionList;
> Open: TAction;
> exit: TAction;
> SD: TSaveDialog;
> DBGrid1: TDBGrid;
> DataSource1: TDataSource;
> Query1: TQuery;
> procedure OpenExecute(Sender: TObject);
> procedure exitExecute(Sender: TObject);
> private
> { Private declarations }
> public
> { Public declarations }
> end;
>
> var
> Form1: TForm1;
>
> implementation
>
> {$R *.DFM}
>
> procedure TForm1.OpenExecute(Sender: TObject);
> Var
> I,K,Cnt,R_SEQ : Integer;
> Num, kNo, kMemo, Name, Dept, Day : string;
> FP : Textfile;
> Buf : String;
> begin
> Cnt := 0;
> if OD.Execute then
> begin
> AssignFile(FP,OD.FileName);
> Reset(FP);
> While not Eof(FP) do
> Begin
> Num :=''; kNo :=''; Buf :=''; kMemo :='';Name:='';
> Dept :=''; Day :='';
> Readln(FP,Buf);
> K:=0;
> for I:=1 to Length(buf) do
> begin
> if buf[I] = ',' then
> K := K + 1
> else
> case K of
> 0 : Num := Num + Buf[i];
> 1 : kNo := kNo + Buf[i];
> 2 : kMemo := kMemo + buf[i];
> 3 : Name := Name + buf[i];
> 4 : Dept := Dept + buf[i];
> 5 : Day := Day + buf[i];
> end;
> kNo := Trim(kNo);
> kMemo := Trim(kMemo);
> Name := Trim(Name);
> Dept := Trim(Dept);
> Day := Trim(Day);
> with Query1 do
> begin
> SQL.Clear;
> SQL.Add('insert into test');
> SQL.Add('values(:r1,:r2,:r3,:r4,:r5)');
> { SQL.Add('update test');
> SQL.Add('Number =:r1,');
> SQL.Add('kMemo =:r2,');
> SQL.Add('Name =:r3,');
> SQL.Add('Dept =:r4,');
> SQL.Add('Day =:r5,');}
> Query1.ParambyName('r1').AsString := kNo;
> Query1.ParambyName('r2').AsString := kMemo;
> Query1.ParambyName('r3').AsString := Name;
> Query1.ParambyName('r4').AsString := Dept;
> Query1.ParambyName('r5').AsString := Day;
> ExecSQL;
> end;
> end;
> end;
> end;
> end;
>
> procedure TForm1.exitExecute(Sender: TObject);
> begin
> close;
> end;
>
> end.