关于文本文档与table |
尚未結案
|
liangzai
初階會員 發表:77 回覆:91 積分:33 註冊:2004-08-13 發送簡訊給我 |
请教各位前辈:
怎样将一个txt文本文档里面的data读到一个table中,比如这样的情况:
1.a.txt
ABCDE ABCDE 5
ABCD ABCD 3
A.BCDEFG A.BCDEFG 8
ABCDEF ABCDEF 10 2.table1
field1 field2 field3 3.希望的结果:
table1
field1 field2 field3
ABCDE ABCDE 5
ABCD ABCD 3
A.BCDEFG A.BCDEFG 8
ABCDEF ABCDEF 10 期待你的指教,急。。。。。谢谢!
|
meta
中階會員 發表:3 回覆:60 積分:57 註冊:2004-06-09 發送簡訊給我 |
//try try..D7下ok!
procedure TForm1.Button2Click(Sender: TObject); var i:Integer; sl:TStringList; begin sl:=TStringList.Create; try sl.LoadFromFile('C:\a.txt'); sl.CommaText:=sl.Text; memo1.Lines.Assign(sl); for i:=0 to memo1.Lines.Count-1 do if i mod 3=0 then AddDB(memo1.Lines[i],memo1.Lines[i十1],memo1.Lines[i十2]); finally sl.Free; end; end; procedure TForm1.AddDB(field1,field2,field3:String); begin try ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('insert into table1'); ADOQuery1.SQL.Add('values(:field1,:field2,:field3)'); ADOQuery1.Parameters.ParamByName('value1').Value:=field1; ADOQuery1.Parameters.ParamByName('value2').Value:=field2; ADOQuery1.Parameters.ParamByName('value3').Value:=field3; ADoQuery1.ExecSQL; except showmessage(AddDb error); end; end;//可以參考站內CommaText、Insert(sql)相關文章 '十'符號怪怪.. //多多指教..多多指教.. |
yorkland
高階會員 發表:2 回覆:138 積分:108 註冊:2004-12-17 發送簡訊給我 |
|
liangzai
初階會員 發表:77 回覆:91 積分:33 註冊:2004-08-13 發送簡訊給我 |
|
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi liangzai,
procedure TForm1.Button1Click(Sender: TObject); var ST1,ST2 : TStrings; I,J : Integer; begin ST1 := TStringList.Create; ST2 := TStringList.Create; ST1.LoadFromFile('C:\Test.txt'); for I := 0 to ST1.Count - 1 do begin ST2.Delimiter := #9; // TAB 鍵 ST2.DelimitedText := ST1[I]; with ClientDataSet1 do begin Append; for J := 0 to FieldCount - 1 do begin try Fields.Fields[J].AsSTring := ST2[J]; except Fields.Fields[J].AsSTring := ''; end; end; PoST end; end; ST2.Free; ST1.Free; end;發表人 -
------
Fishman |
liangzai
初階會員 發表:77 回覆:91 積分:33 註冊:2004-08-13 發送簡訊給我 |
|
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi liangzai, 1.ST2[J] 型態為 String 如何進行 ST2[J] - 1 的運算? 2.以下為 TSQLTable 的 Help,看了應該就有答案了 TSQLTable represents a database table that is accessed using dbExpress. Unit SqlExpr Description Use TSQLTable to represent a table on a database server that is accessed via TSQLConnection. TSQLTable generates a query to fetch all of the rows and columns in a table you specify. You can add a TSQLTable component to a form or data module at design time, or create one dynamically at runtime. TSQLTable is a unidirectional dataset. Unlike other datasets, unidirectional datasets do not buffer multiple records in memory. Because of this, you can only navigate using the First and Next methods. There is no built-in editing support: you can only edit the data in an SQL table by explicitly creating an SQL UPDATE command or by connecting the table to a client dataset using a provider. Features that require buffering multiple records, such as filters or lookup fields, are not available. Before you can use a TSQLTable component, it must be connected to the database server. Therefore, the first step to take when working with TSQLTable is to set the SQLConnection property. Once the dataset is connected to a server, use the TableName property to specify the table this component represents. ----------------------------------
小弟才疏學淺,若有謬誤尚請不吝指教
----------------------------------
------
Fishman |
liangzai
初階會員 發表:77 回覆:91 積分:33 註冊:2004-08-13 發送簡訊給我 |
procedure TForm1.BitBtn1Click(Sender: TObject);
var
ST1,ST2 : TStringlist;
I,J : Integer;
begin
ST1:=TStringList.Create;
ST2:=TStringList.Create;
ST1.LoadFromFile('C:\1.txt');
for I:=0 to ST1.Count-1 do
begin
self.Memo1.Lines.Add(ST1[I]);
ST2.Delimiter:=#9; // TAB 鍵
ST2.DelimitedText:=ST1[I];
for J:=0 to st2.Count-1 do
self.Memo2.Lines.Add(ST2[J]);
with self.ClientDataSet1 do
begin
Active:=true;
Append;
for J:=0 to st2.Count-1 do
begin
try
Fields.Fields[J].AsSTring:=ST2[J];
except
Fields.Fields[J].AsSTring:='';
end;
end;
post;
end;
end;
ST2.Free;
ST1.Free; end; end.
|
liangzai
初階會員 發表:77 回覆:91 積分:33 註冊:2004-08-13 發送簡訊給我 |
|
liangzai
初階會員 發表:77 回覆:91 積分:33 註冊:2004-08-13 發送簡訊給我 |
|
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi liangzai,
1. ClientDataSet 未事先定義欄位,所以 FieldCount 為 0,可先行定義好或是改用你的寫法 2. 我用 SQLTable 一直有問題,因此改用 SQL Query 已成功寫入 Oracle,但是必須加入 ClientDataSet1..ApplyUpdates(-1); 進行批次更新 procedure TForm1.Button1Click(Sender: TObject); var ST1,ST2 : TStrings; I,J : Integer; begin ST1 := TStringList.Create; ST2 := TStringList.Create; ST1.LoadFromFile('C:\Test.txt'); for I := 0 to ST1.Count - 1 do begin ST2.Delimiter := #9; // TAB 鍵 ST2.DelimitedText := ST1[I]; with ClientDataSet1 do begin Append; for J := 0 to FieldCount - 1 do begin try Fields.Fields[J].AsString := ST2[J]; except Fields.Fields[J].AsString := ''; end; end; Post; end; end; ClientDataSet1.ApplyUpdates(-1); ST2.Free; ST1.Free; end;---------------------------------- 小弟才疏學淺,若有謬誤尚請不吝指教 ----------------------------------
------
Fishman |
liangzai
初階會員 發表:77 回覆:91 積分:33 註冊:2004-08-13 發送簡訊給我 |
|
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi liangzai, 其實不管是 Oracle 或是 SQL Server 原理都是一樣的 TSQLConnection(OracleConnection) --> TSQLTable(tablename)/TSQLQuery(select * from tablename) --> TDataSetPrvider --> TClientDataSet 在對 ClientDataSet 進行 Append 與 Post、最後再 ApplyUpdate 進入資料庫 ----------------------------------
小弟才疏學淺,若有謬誤尚請不吝指教
----------------------------------
------
Fishman |
liangzai
初階會員 發表:77 回覆:91 積分:33 註冊:2004-08-13 發送簡訊給我 |
|
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi liangzai,
這就是為何我會用 for J := 0 to FieldCount - 1 do begin try Fields.Fields[J].AsString := ST2[J]; except Fields.Fields[J].AsString := ''; end; end; 你也可以改成這樣 for J:=0 to st2.Count-1 do begin try Fields.Fields[J].AsString:=ST2[J]; except end;---------------------------------- 小弟才疏學淺,若有謬誤尚請不吝指教 ----------------------------------
------
Fishman |
liangzai
初階會員 發表:77 回覆:91 積分:33 註冊:2004-08-13 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |