备份数据表为insert 脚本
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 脚本的更多相关文章
- 将表数据生成Insert脚本
set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- =============================================-- Author ...
- 从SqlServer现有数据生成Insert脚本
步骤1,打开"Generate and Publish Objects"向导.右键点击要导出数据的数据库,选择Taks->GenerateScript 步骤2,选择要导出数据 ...
- SqlServer 导出指定表数据 生成Insert脚本
版权声明:本文为博主原创文章,未经博主允许不得转载.
- MSSQL的表备份成INSERT脚本的存储过程
USE [SupplyChain]GO/****** Object: StoredProcedure [dbo].[ExpData] Script Date: 2015-12-18 10:23:08 ...
- 使用SQL语句创建SQL数据脚本(应对万网主机部分不支持导出备份数据)
1.查询待导出表Ad中的数据. SELECT * FROM [DB_Temp].[dbo].[Ad] 2.编写存储过程. --将表数据生成SQL脚本的存储过程 CREATE PROCEDURE dbo ...
- 在sqlServer中把数据导出为insert脚本
有时候为了把数据导出为insert脚本,不得不用一些小工具,或者通过自己写存储过程来完成这一操作.其实SqlServer本身就有这种功能.以下是详细步骤:
- Mysql定时备份数据脚本
项目集群搭建完成,数据库虽有做主从同步,但考虑到数据安全性,为了满足这个需求那么要每天对数据备份处理, 但每天手动进行备份处理太过于被动,而且白天用户访问,会有数据变化以及在备份时会影响服务器正常运行 ...
- sql server中备份数据的几种方式
当我们在写sql脚本要对数据表中的数据进行修改的时候,为了防止破坏数据,通常在开发前都会对数据表的数据进行备份,当我们sql脚本开发并测试完成后,再把数据恢复回来. 目前备份数据,我常用的方法有以下几 ...
- SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享
SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享 第一步建库和建表 USE [master] GO CREATE DATABASE [MonitorElapsedHighSQL] G ...
随机推荐
- mac 用 brew
mac 下用 brew 安装插件, 有重复的不会再次安装,比xmap模式好
- python之高性能网络编程并发框架eventlet实例
http://blog.csdn.net/mingzznet/article/details/38388299 前言: 虽然 eventlet 封装成了非常类似标准线程库的形式,但线程和eventle ...
- 1400 - "Ray, Pass me the dishes!"
哈哈,原来题意看错了,但有多个解的时候,输出起点靠前的,如果起点一样,则输出终点靠前的,修改后AC的代码如下: #include <cstdio> #include <iostrea ...
- 如何理解IoC/DI
IoC:Inversion of Control,控制反转DI:Dependency Injection,依赖注入 要理解上面两个概念,就必须搞清楚如下的问题: 参与者都有谁?依赖:谁依赖于谁?为什么 ...
- pmf,cpmf,pdf,cdf,iid的解释
- Vim插件列表
01.helm(Vim-Swoop) 02.ap/vim-buftabline 03.wesleyche/SrcExpl 04.vim proc 05.vim shell 06.dhruvasagar ...
- VS2005工程增加SDK
客户最近发过来一个VS2005的工程,此工程是基于Pocket PC 2003(ARMV4),需要改为我们WINCE6.0系统对应的SDK,下面具体说明如何添加新的SDK. 选择configurati ...
- string evaluated instead to freemarker.template.SimpleScalar
[2015-09-06 09:07:32.879] ERROR [6B68DD09CE6FECFE20936CA3C6D560AD:http-bio-8087-exec-8] o.a.s.v.free ...
- POI根据EXCEL模板,修改内容导出新EXCEL (只支持HSSF)
package excelPoiTest; import java.io.File; import java.io.FileInputStream; import java.io.FileOutput ...
- ASP.NET中的FileUpload文件上传控件的使用
本篇文章教大家如何将客户端的图片或者文件上传到服务器: 无论是上传图片(.jpg .png .gif等等) 文档(word excel ppt 等等). 第一步:放入以下三个控件 Image控件,Fi ...