博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
备份数据表为insert 脚本
阅读量:6233 次
发布时间:2019-06-22

本文共 2759 字,大约阅读时间需要 9 分钟。

unit Unit1;

interface

uses

Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB,
Datasnap.DBClient;

type

TForm1 = 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;

var

Form1: TForm1;

implementation

{$R *.dfm}

uses untDB;

procedure TForm1.Button1Click(Sender: TObject);

begin
ExportData('bas_kind,bas_goods');
end;

procedure TForm1.Button2Click(Sender: TObject);

begin
ImportData(ExtractFilePath(Application.ExeName) + 'export.sql', 'bas_kind,bas_goods');
end;

procedure TForm1.ExportData(const tableNames: string);

var
sql, err: string;
sl, tables: TStringList;
i, h: Integer;
fieldNames, values: string;
function GetValue(field: TField): string;
begin
case field.DataType of
ftString, ftTimeStamp:
begin
Result := QuotedStr(field.AsString);
end;
ftBoolean:
begin
case field.AsBoolean of
true: Result := '1';
false: Result := '0';
end;
end;
else
if not VarIsNull(field.Value) then
begin
Result := VarToStr(field.Value);
end
else
Result := 'null';
end;
end;

begin

if tableNames = '' then
exit;
tables := TStringList.Create;
tables.Delimiter := ',';
tables.DelimitedText := tableNames;
sl := TStringList.Create;
sl.Clear;
for h := 0 to tables.Count - 1 do
begin
sql := 'select * from ' + tables[h];
if frmDB.QuerySQL(sql, cds, err) and (not cds.IsEmpty) then
begin
cds.First;
while not cds.Eof do
begin
fieldNames := '';
values := '';
for i := 0 to cds.FieldCount - 1 do
begin
if fieldNames = '' then
begin
fieldNames := cds.Fields[i].FieldName;
end
else
begin
fieldNames := fieldNames + ',' + cds.Fields[i].FieldName;
end;
if values = '' then
begin
values := GetValue(cds.Fields[i]);
end
else
begin
values := 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);

var
cmd, tables: TStringList;
err, sql: string;
i: Integer;
begin
if (not FileExists(fileName)) or (tableNames = '') then
exit;
tables := TStringList.Create;
tables.Delimiter := ',';
tables.DelimitedText := tableNames;
for i := 0 to tables.Count - 1 do
begin
sql := '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.

转载地址:http://ixqna.baihongyu.com/

你可能感兴趣的文章
<<、|=、&的小例子
查看>>
愿Linux红帽旋风吹得更加猛烈吧!
查看>>
Secret Code
查看>>
Vue动态组件
查看>>
ES2017异步函数现已正式可用
查看>>
DBA-io
查看>>
【转】批处理常用符号详解
查看>>
Uncaught TypeError: jQuery.i18n.browserLang is not a function
查看>>
JavaScript中的闭包详解
查看>>
【JSP】JSP Action动作标签
查看>>
iOS:CoreText的常用语法
查看>>
dropify,不错的图片上传预览插件
查看>>
为什么都不写博
查看>>
希腊字母表
查看>>
httpd配置文件httpd.conf规则说明和一些基本指令
查看>>
python中self cls init的理解
查看>>
java:类集操作总结
查看>>
Flake8学习
查看>>
SpringBoot项目eclipse运行正常maven install打包启动后报错ClassNotFoundException
查看>>
ASP.NET Core的身份认证框架IdentityServer4(9)-使用OpenID Connect添加用户认证
查看>>