paradox資料庫的建立與資料操作
阿新 • • 發佈:2020-08-02
paradox資料庫是delphi自帶的.db資料庫檔案,所在目錄即是資料庫,一個表是一個.db檔案
paradox資料型別簡表:
用Ttable建立paradox資料庫
begin with Table1 do begin Active := False; DatabaseName := ''; TableType := ttParadox; TableName := 'CustInfo.db'; with FieldDefs do begin Clear; Add('Field1', ftInteger, 0, True); Add('Field2', ftString, 30, False);end; CreateTable; end; end;
用TQuery,sql語句建立paradox資料庫
var TempQuery:TQuery; DefField :String; TableName:String; CreateSQL:String; begin CreateSQL:='Create Table "%s" (%s)'; Deffield := ' Field1 Integer, Field2 CHAR(30) '; TableName := 'C:\DB01.DB'; TempQuery := TQuery.Create(Application); with TempQuery dobegin try SQL.Text:=Format(CreateSQL,[tableName,deffield]); ExecSQL; finally Free; end; end; end;
為paradox資料表插入資料:
begin tbl1.DatabaseName:=ExtractFilePath(Application.ExeName)+'db'; tbl1.TableType:=ttParadox; tbl1.TableName:='User'; tbl1.Close; tbl1.Open; while not tbl1.Eof do tbl1.Delete; //刪除原有的資料for i:=1 to 20 do begin tbl1.Insert; tbl1.FieldByName('fID').AsInteger:=i; tbl1.FieldByName('fName').AsString:='段改陽'+inttostr(i); tbl1.FieldByName('ftype').AsInteger:=i; tbl1.FieldByName('fdescrip').AsString:='fdescrip段改陽'+inttostr(i); end; tbl1.Post; end;
append用法:
type TForm1 = class(TForm) Table1: TTable; DataSource1: TDataSource; DBGrid1: TDBGrid; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin with Table1 do begin Active := False; DatabaseName := ''; TableType := ttParadox; TableName := 'DOCTORINF23.db'; if not Table1.Exists then begin with FieldDefs do begin Clear; with AddFieldDef do begin Name := '姓名'; DataType := ftString; Required := True; Size := 10; end; with AddFieldDef do begin Name := '年齡'; DataType := ftInteger; end;//建立欄位定,利用AddFieldDef方法新增一個新的TFieldDef物件 with AddFieldDef do begin Name := '職稱'; DataType := ftString; Required := True; Size := 10; end; end; with IndexDefs do begin Clear; with AddIndexDef do begin Name := 'MYINDEX'; Fields := '姓名'; Options := [ixPrimary]; end; end; //建立索引 CreateTable; end; end; Table1.Open; Table1.Edit; Table1.FieldByName('姓名').AsString:='劉延'; Table1.FieldByName('年齡').AsInteger:=22 ; Table1.FieldByName('職稱').AsString:='醫師'; //---添加了一條記錄 ,append開始新增第二條記錄 Table1.Append; Table1.Edit; Table1.FieldByName('姓名').AsString:='楊曉'; Table1.FieldByName('年齡').AsInteger:=25 ; Table1.FieldByName('職稱').AsString:='醫師'; DBGrid1.DataSource:=DataSource1; Table1.Active :=True; end; end.