备份数据表为insert 脚本

时间:2022-09-15 09:44:41

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.

备份数据表为insert 脚本的更多相关文章

  1. 将表数据生成Insert脚本

    set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- =============================================-- Author ...

  2. 从SqlServer现有数据生成Insert脚本

    步骤1,打开"Generate and Publish Objects"向导.右键点击要导出数据的数据库,选择Taks->GenerateScript 步骤2,选择要导出数据 ...

  3. SqlServer 导出指定表数据 生成Insert脚本

    版权声明:本文为博主原创文章,未经博主允许不得转载.

  4. MSSQL的表备份成INSERT脚本的存储过程

    USE [SupplyChain]GO/****** Object: StoredProcedure [dbo].[ExpData] Script Date: 2015-12-18 10:23:08 ...

  5. 使用SQL语句创建SQL数据脚本(应对万网主机部分不支持导出备份数据)

    1.查询待导出表Ad中的数据. SELECT * FROM [DB_Temp].[dbo].[Ad] 2.编写存储过程. --将表数据生成SQL脚本的存储过程 CREATE PROCEDURE dbo ...

  6. 在sqlServer中把数据导出为insert脚本

    有时候为了把数据导出为insert脚本,不得不用一些小工具,或者通过自己写存储过程来完成这一操作.其实SqlServer本身就有这种功能.以下是详细步骤:

  7. Mysql定时备份数据脚本

    项目集群搭建完成,数据库虽有做主从同步,但考虑到数据安全性,为了满足这个需求那么要每天对数据备份处理, 但每天手动进行备份处理太过于被动,而且白天用户访问,会有数据变化以及在备份时会影响服务器正常运行 ...

  8. sql server中备份数据的几种方式

    当我们在写sql脚本要对数据表中的数据进行修改的时候,为了防止破坏数据,通常在开发前都会对数据表的数据进行备份,当我们sql脚本开发并测试完成后,再把数据恢复回来. 目前备份数据,我常用的方法有以下几 ...

  9. SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享

    SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享 第一步建库和建表 USE [master] GO CREATE DATABASE [MonitorElapsedHighSQL] G ...

随机推荐

  1. 子代选择器(>)后代选择器(' ')的区别

    子代选择器是指紧接着父级的那个标签,如:container>a指的是紧接着container后面的第一个a(儿子级别的,孙子或者之后的a是不能生效的) 后代选择器是用空格分开的,如:contai ...

  2. web开发者谷歌浏览器常用插件

    1.Allow-Control-Allow-Origin    安装此插件解决跨域问题,在本地起服务器可访别的域的数据. 需在Access-Control-Expose-Headers加上Allow- ...

  3. Rhel6-集群管理(luci&&ricci)配置文档

    理论基础: User → HA →     Lb    → web → sql → 分布式filesystem ->磁盘I/O 用户   高可用 负载均衡    应用   数据库      mf ...

  4. localtunnel.me 原理流程浅析

      本文採用知识共享署名-同样方式共享 4.0 国际许可协议进行许可. 近期公司的项目须要一个后台server,要求能将内网的WEBserver暴露在外网上.于是.就在网上到处搜开源项目.找到两个口碑 ...

  5. 使用jQuery实现跨域提交表单数据

    我们在WEB开发中有时会遇到这种情况,比如要从A网站收集用户信息,提交给B网站处理,这个时候就会涉及到跨域提交数据的问题.本文将给您介绍如何使用jQuery来实现异步跨域提交表单数据.   在jQue ...

  6. asp.net弹出多个模态窗口

    asp.net中无限制弹出模态窗口 特点: 1.       可以在模态窗口上再弹出模态窗口,不限制次数 2.       弹出窗口的支持拖放,及调整大小 3.       弹出窗口关闭后可以动态控制 ...

  7. HTML超连接的使用

    1,基本语法:<a href="" target="打开方式" name ="页面锚点名称">连接文字或者图片</a&gt ...

  8. react-native上手篇

    根据公司发展,后期可能要做APP开发,所以了解一下react-native.之前工作用过react,所以想想应该不会太难.(结果配置环境和demo就搞了一天!) 1,搭建环境 官网地址 1,Node( ...

  9. 左查询left join on简单总结

    应用场景分析:(个人观点,欢迎小祖宗们指正补充) 适合存在父子关系的单表,以及多表的查询   话不多说上代码 代码:mapper里的sql 表名字段什么的本来是单独集中配置的,现在还原到sql中了 & ...

  10. 004-js-md5

    参看地址:https://www.bootcdn.cn/blueimp-md5/