zoukankan      html  css  js  c++  java
  • 备份数据表为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.

  • 相关阅读:
    bq25896 IINDPM 及 無 IINDPM 時的 regsiter
    不同狀況下的 bq25896 register
    bq25896 charging status CHRG_STAT register 0xB
    快充 IC BQ25896 如何判斷 手機插著 adapter 充電器時,adapter Iout 大於限制,adapter Vout 小於 限制,導致 battery 不但沒充電且還需放電。
    在 kernel 下打出 有帶參數的log。 怪異現象與解決方式。
    mtk flash tool,Win7 On VirtualBox
    java 去最后一位字符 str.substring(0,str.length()-1)
    Windows下Oracle的下载与安装及配置
    关于Java中SQL语句的拼接规则
    <id name="ID"> <generator class="assigned" /> </id>
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/3796241.html
Copyright © 2011-2022 走看看