How to back up the data table in Delphi for Insert script

Source: Internet
Author: User
Tags exit count insert integer
Unit Unit1; Interface uses Winapi.windows, Winapi.messages, System.sysutils, System.variants, system.classes, Vcl.graphics, Vcl.C
    
Ontrols, 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; The 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.
A; 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.

Author:cnblogs Yong South Delphi

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Delphi/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.