我在一本书中看到可以把一个分栏格式的文本文件读入数据库中,我就用记事本做了一个文本文件.格式如下.
china abc
usa bcd
canada efg
france hij
但我发现程序并没有以分栏格式把数据写入数据库,而是把china abc做为同一行读入一个字段中,如何才能做出有分栏格式的文本文件.下面是该程序的代码.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
DBGrid1: TDBGrid;
Table1: TTable;
DataSource1: TDataSource;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
table1.EmptyTable ;
end;procedure TForm1.Button1Click(Sender: TObject);
var
a:textfile;
statename:string[20];
capitalname:string[20];
begin
table1.Open ;
assignfile(a,'c:\v.txt') ;
reset(a);
try
while not eof(a) do
begin
readln(a,statename,capitalname);
table1.Insert ;
table1['contry']:=statename;
table1['captin']:=capitalname;
table1.Post;
table1.Active:=true;
end;
finally
closefile(a);
end;
end;
end.
在这段代码中table1.Open ;从字面上看是打开table1但不知为什么要用他,什么时候才用他.
还有table1['contry']:=statename;
table1.Post;又是做什么用的.
谢谢!!!!!!!!!!!!!
china abc
usa bcd
canada efg
france hij
但我发现程序并没有以分栏格式把数据写入数据库,而是把china abc做为同一行读入一个字段中,如何才能做出有分栏格式的文本文件.下面是该程序的代码.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
DBGrid1: TDBGrid;
Table1: TTable;
DataSource1: TDataSource;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
table1.EmptyTable ;
end;procedure TForm1.Button1Click(Sender: TObject);
var
a:textfile;
statename:string[20];
capitalname:string[20];
begin
table1.Open ;
assignfile(a,'c:\v.txt') ;
reset(a);
try
while not eof(a) do
begin
readln(a,statename,capitalname);
table1.Insert ;
table1['contry']:=statename;
table1['captin']:=capitalname;
table1.Post;
table1.Active:=true;
end;
finally
closefile(a);
end;
end;
end.
在这段代码中table1.Open ;从字面上看是打开table1但不知为什么要用他,什么时候才用他.
还有table1['contry']:=statename;
table1.Post;又是做什么用的.
谢谢!!!!!!!!!!!!!
解决方案 »
- delphi调用webservice客户端不生成代码
- 一个线程的问题
- 在动态库中使用XML的问题?
- 请问在DELPHI中如何用程序判断系统中有某个程序在运行,如在运行,怎么才能用程序去关闭它?假设程序是af.exe,位于"c:\af.exe".多谢了.
- 如何判断输入edit框中的数据是否是整型数据
- 当我在SaveDialog 中点取消时,却不能关闭“另存为“的窗体,我要怎么做才能关掉它
- 大伙能帮我一下吗(送分了)?
- vb代码转delphi,请大神们献策!
- 有没有系统备份软件,适用于WIN2000的。GHOST好像不能。
- 如果用SQL函数截取DATETIME类型字段中的DATE值?(返回结果仍得为日期型)
- 郁闷啊,高手来帮忙!!!!!!!!
- 在delphi中建立了excel对象,写什么代码把它给释放掉?
FileStrings:TStrings;
i:integer;
country,captin:string;
begin
FileStrings:=TStrings.Create;
FileStrings.LoadFromFile('a.txt');
table1.Open ;//打开表以使其可写记录
for i:=0 to FileStrings.Count-1 do
begin
country:=copy(FileStrings[i],1,10); //假设你的国家字段与下一个字段是用空格分开的,并且第一个字段最长不超过10个字符
captin:=copy(FileStrings[i],11,Length(FileStrings[i]-10);//第二个字段
table1.Insert ;
table1['contry']:=country;
table1['captin']:=captin;
table1.Post;//保存当前记录到磁盘
end;
FileStrings.Free;
function TForm_server_cs.TxtToPubDB(tablename:string;key:string;Txtname:TFileName):integer;
var
F1:Textfile;
i,j,k:integer;
TmpStr,TmpTag:string;
ch:char;
begin
try
AssignFile(F1,txtname);
reset(f1);
read(f1,ch);
if DataSet_tmp.Active then DataSet_tmp.Close;
DataSet_tmp.CommandText:='select * from '+tablename+' where 1=2';
DataSet_tmp.Open; while ch<>#26 do begin DataSet_tmp.Append;
for i:=0 to DataSet_tmp.FieldCount-1 do begin
TmpStr:='';
while (ch<>#9) do begin //#9 is tab
if ch<>#13 then begin //#13 is enter
if ch<>#26 then begin //#26 is end
Tmpstr:=TmpStr+ch;
read(F1,ch);
end else begin
closefile(f1); if testFlag=true then showmessage('ok');
//从DataSet_tmp往SourceTable中加入数据
DataSet_tmp.First;
if not DataSet_tmp.Active then DataSet_tmp.Open;
for j:=0 to DataSet_tmp.RecordCount-1 do begin
//如果在SourceTable中已存在该商品的记录则更新,否则添加新记录
if DataSet_Access.Active then DataSet_Access.Close;
DataSet_Access.CommandText:='select * from '+tablename+' where '+key+'='''+DataSet_tmp.fieldByname(Key).AsString+'''';
DataSet_Access.Open; if (tablename='salelist') or (tablename='shopneedlist') or (tablename='comelist') then begin
DataSet_Access.Append;
for k:=0 to DataSet_Access.FieldCount-1 do begin
if uppercase(trim(DataSet_Access.FieldDefList.FieldDefs[k].Name))<>uppercase('iListNo') then begin
if uppercase(trim(DataSet_Access.FieldDefList.FieldDefs[k].Name))<>uppercase('ddate') then begin
if uppercase(trim(DataSet_Access.FieldDefList.FieldDefs[k].Name))<>uppercase('bSendAcc') then
DataSet_Access.Fields[k].AsString:=DataSet_tmp.Fields[k].AsString
else
DataSet_Access.Fields[k].AsBoolean:=false;
end;
end;
end;
DataSet_Access.Post
end else begin
if not DataSet_Access.IsEmpty then begin
DataSet_Access.Edit;
for k:=0 to DataSet_Access.FieldCount-1 do begin
if uppercase(trim(DataSet_tmp.FieldDefList.FieldDefs[k].Name))<>uppercase(key) then begin
if uppercase(trim(DataSet_tmp.FieldDefList.FieldDefs[k].Name))<>uppercase('iListNo') then begin
if uppercase(trim(DataSet_Access.FieldDefList.FieldDefs[k].Name))<>uppercase('ddate') then begin
if uppercase(trim(DataSet_Access.FieldDefList.FieldDefs[k].Name))<>uppercase('bSendAcc') then
DataSet_Access.Fields[k].AsString:=DataSet_tmp.Fields[k].AsString
else
DataSet_Access.Fields[k].AsBoolean:=false;
end;
end;
end;
end;
DataSet_Access.Post;
end else begin
if uppercase(copy(tablename,1,6))<>uppercase('gpinfo') then begin
DataSet_Access.Append;
for k:=0 to DataSet_Access.FieldCount-1 do begin
if uppercase(trim(DataSet_Access.FieldDefList.FieldDefs[k].Name))<>uppercase('iListNo') then begin
if uppercase(trim(DataSet_Access.FieldDefList.FieldDefs[k].Name))<>uppercase('ddate') then begin
if uppercase(trim(DataSet_Access.FieldDefList.FieldDefs[k].Name))<>uppercase('bSendAcc') then
DataSet_Access.Fields[k].AsString:=DataSet_tmp.Fields[k].AsString
else
DataSet_Access.Fields[k].AsBoolean:=false;
end;
end;
end;
DataSet_Access.Post;
end else
Application.MessageBox(Pchar('连锁店'+TableName+'中的商品'
+DataSet_tmp.fieldByname(Key).AsString
+'是新品,不能插入!')
,'系统信息',MB_ok); end;
end;
DataSet_tmp.Next;
end;////从MD_gpinfo往Gpinfo中加入数据完成
//if DataSet_Access.State in [dsEdit,dsInsert] then DataSet_Access.Post;
result:=DataSet_tmp.RecordCount;
DataSet_tmp.Close;
exit;
end;
end else
read(f1,ch);
end;
read(F1,ch); {当字段名为iListNo,ddate时,则不填,iListNo是一个自动增量}
if uppercase(trim(DataSet_tmp.FieldDefList.FieldDefs[i].Name))<>uppercase('iListNo') then begin
if uppercase(trim(DataSet_tmp.FieldDefList.FieldDefs[i].Name))<>uppercase('ddate') then begin
if uppercase(trim(DataSet_tmp.FieldDefList.FieldDefs[i].Name))<>uppercase('bSendAcc') then
DataSet_tmp.Fields[i].AsString:=trim(TmpStr)
else
DataSet_tmp.Fields[i].AsBoolean:=false;
end;
end; //if testflag=true then showmessage('field['+SourceTable.FieldDefList.FieldDefs[i].Name+']'+SourceTable.Fields[i].AsString);
end;//end of (for i:=0 to SourceTable.FieldCount-1 do)
end;//end of (if not eof(f1) then
except
closefile(f1);
MessageDlg( '接收数据失败!',mtError,[mbOk],0);
end;