Delphi中如何備份資料表為insert 指令碼

來源:互聯網
上載者:User
unit Unit1;    interface    usesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB,Datasnap.DBClient;    typeTForm1 = class(TForm)cds: TClientDataSet;Button1: TButton;Button2: TButton;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);private{ Private declarations }procedure ExportData(const tableNames: string);procedure ImportData(const fileName, tableNames: string);public{ Public declarations }end;    varForm1: TForm1;    implementation    {$R *.dfm}    uses untDB;    procedure TForm1.Button1Click(Sender: TObject);beginExportData('bas_kind,bas_goods');end;    procedure TForm1.Button2Click(Sender: TObject);beginImportData(ExtractFilePath(Application.ExeName) + 'export.sql', 'bas_kind,bas_goods');end;    procedure TForm1.ExportData(const tableNames: string);varsql, err: string;sl, tables: TStringList;i, h: Integer;fieldNames, values: string;function GetValue(field: TField): string;begincase field.DataType offtString, ftTimeStamp:beginResult := QuotedStr(field.AsString);end;ftBoolean:begincase field.AsBoolean oftrue: Result := '1';false: Result := '0';end;end;elseif not VarIsNull(field.Value) thenbeginResult := VarToStr(field.Value);endelseResult := 'null';end;end;    beginif tableNames = '' thenexit;tables := TStringList.Create;tables.Delimiter := ',';tables.DelimitedText := tableNames;sl := TStringList.Create;sl.Clear;for h := 0 to tables.Count - 1 dobeginsql := 'select * from ' + tables[h];if frmDB.QuerySQL(sql, cds, err) and (not cds.IsEmpty) thenbegincds.First;while not cds.Eof dobeginfieldNames := '';values := '';for i := 0 to cds.FieldCount - 1 dobeginif fieldNames = '' thenbeginfieldNames := cds.Fields[i].FieldName;endelsebeginfieldNames := fieldNames + ',' + cds.Fields[i].FieldName;end;if values = '' thenbeginvalues := GetValue(cds.Fields[i]);endelsebeginvalues := values + ',' + GetValue(cds.Fields[i]);end;end;sl.Add('insert into ' + tables[h] + '(' + fieldNames + ') values (' +values + ')');cds.Next;end;end;end;sl.SaveToFile(ExtractFilePath(Application.ExeName) + 'export.sql');sl.Free;tables.Free;end;    procedure TForm1.ImportData(const fileName, tableNames: string);varcmd, tables: TStringList;err, sql: string;i: Integer;beginif (not FileExists(fileName)) or (tableNames = '') thenexit;tables := TStringList.Create;tables.Delimiter := ',';tables.DelimitedText := tableNames;for i := 0 to tables.Count - 1 dobeginsql := 'truncate table ' + tables[i];frmDB.ExecuteSQL(sql, err);end;tables.Free;    cmd := TStringList.Create;cmd.LoadFromFile(fileName);frmDB.ExecuteSQL(cmd.Text, err);cmd.Free;end;    end.

Author:cnblogs 詠南 delphi

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Delphi/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.