备份数据表为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 ...
随机推荐
- 1.Getting Started with ASP.NET MVC 5
Getting Started Start by installing and running Visual Studio Express 2013 for Web or Visual Studio ...
- xcode 树形管理 cocos2d-x的资源
把资源以目录的形式加入xcode中, 同时, 在加入时, 选择"Create Folder References for any added folders", 而不是默认的&q ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-007-设置LDAP server比较密码(contextSource、root()、ldif()、)
一.LDAP server在哪 By default, Spring Security’s LDAP authentication assumes that the LDAP server is li ...
- Qt: 访问容器(三种方法,加上for循环就四种了)good
#include <iostream>#include <QString>#include <QList>#include <QListIterator> ...
- 186. Reverse Words in a String II
题目: Given an input string, reverse the string word by word. A word is defined as a sequence of non-s ...
- Android Paint中setTextSize
界面适配的时候发现Paint.setTextSize与TextView.setTextSize传入的单位不一致.Paint.setTextSize传入的单位是px,TextView.setTextSi ...
- python 简单示例说明os.walk和os.path.walk的不同
import os,os.path def func(arg,dirname,names): for filespath in names: print os.path.join(dirname,fi ...
- ActiveMQ可靠性机制
消息的签收(Acknowledgment): 客户端成功接收一条消息的标志是这条消息被签收. 成功接收一条消息一般包括如下三个阶段: (1) 客户端接收消息 (2) 客户端处理消息 (3) 消息 ...
- 如何写mysql的定时任务
什么是事件: 一组SQL集,用来执行定时任务,跟触发器很像,都是被动执行的,事件是因为时间到了触发执行,而触发器是因为某件事件(增删改)触发执行: 查看是否开启: show variables li ...
- 宏HASH_DELETE
HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, fold, bpage); NAME 可理解为 void* next /********** ...