Incoming data sets, generating SQL scripts for corresponding tables

Source: Internet
Author: User
Tags locale mdb database access database

Delphi, the incoming data set, generates his SQL script.

//Create Access Table 1: Build script based on data source structureFunction createaccesstable (dsource:tdataset; stablename:string; Signorefields:array ofstring): string;    Var sql:tstringlist;    Ofield:tfield;    I, N:integer; sf:string; Begin Result:="'; SQL:=tstringlist.Create; With SQL DoTry N:=0; //Sql. text:='Create Table'+stablename+' '; Sql. ADD ('( '); For I:=0  todsource.fieldcount-1  DoBegin Ofield:=Dsource.fields[i]; ifIndexofstrarray (Ofield.fieldname,signorefields) >=0  ThenContinue; //            ifN>0  Thensf:=',' Elsesf:="'; SF:=sf+ofield.fieldname+' '+getfieldtypestring (Ofield) +'Null'; Sql. ADD ('    '+SF);        INC (n);        End; //Add primary Key        //Sql.add (', Primary Key (Sstoreid, Sitemid) ');        //EndSql. ADD (') '); //Completeresult:=SQL.        Text; Sql.    Free; Except on E:exception DoBegin Try SQL. Free;            Except End; Raise Exception.Create('[CreateAccessTable-1] Creating an Access table'+stablename+'Error! '+# -+e.message);    End; End; End;//Create an Access table, perform 2Function createaccesstable (dsource:tdataset; Qry_access:tadoquery;    stablename:string; Signorefields:array ofString; ltrydropexist:boolean=True): Boolean; overload;labelLbl_iprocend;                    Var Qry:tadoquery;    s, S2, Sscript, serror:string;    I:integer; L, Ltrans:boolean; Begin Result:=false; qry:=qry_access; With Qry DoTry serror:="'; //Check if this table existsl:=tableexists (connection, stablename); //Delete the original table and check if there are any tablesIf ltrydropexist Then beginClose; If Ltrydropexist and ((Qry.connectionstring="') and (not Qry.Connection.InTransaction)) ThenBegin Connection.begintrans; Ltrans:=True;            End; //found, removed            ifL ThenTry Dosql (Qry,'Drop Table'+stablename); Except on E:exception Do ;          End; End        Else ifL Then begin                        //no deletion, there are already tables, ignoring            GotoLbl_iprocend; End; //Create a tablesscript:=createaccesstable (Dsource,stablename,signorefields); ifsscript="'  ThenRaise Exception.Create('[Createaccesstable-1-2] Error generating script! '); Sql. Text:=Sscript; {$IFDef Debugclipboard}Clipboard.astext:=sql.text;{$EndIF}    //DebugExecsql; //Lbl_iprocend:ifLtrans ThenConnection.committrans; Result:=True; Except on E:exception DoBeginifLtrans ThenConnection.rollbacktrans; Serror:='[CreateAccessTable-2] Executing an Access table'+stablename+'Create Error! '+# -+E.message;    End;    End; ifSerror<>"'  ThenRaise Exception.Create(serror); End;//Create an Access databaseFunction Createaccessdb (sfilename:string; spassword:string="'): Boolean; Const//default language flag locale identifier=2057solelink='provider=microsoft.jet.oledb.4.0; Locale identifier=2057;data source=%s; Jet oledb:database password=%s'; Var createaccess:olevariant; Begin Result:=False; Try createaccess:= Createoleobject ('ADOX. Catalog'); Createaccess.Create(Format (Solelink,[sfilename,spassword])); Createaccess:=null; Result:=True; Except on E:exception DoRaise Exception.Create('[Createaccessdb] Creating an MDB database'+sfilename+'Error! '+# -+e.message); End; End;//Create a new fieldfunctionCreatenewfield (odb:tdataset; sfieldname:string; otype:tfieldtype; nsize:integer=-1): Tfield;varD:tdataset;beginResult:=Nil; d:=ODB;  CaseOtype ofFtstring:result:=tstringfield.Create(d); Ftinteger:result:=tintegerfield.Create(d); Ftfloat:result:=tfloatfield.Create(d); Ftboolean:result:=tbooleanfield.Create(d); Ftdatetime:result:=tdatetimefield.Create(d); End; Result. FieldName:=Sfieldname; ifNsize>0  ThenResult. size:=nSize; //result. SetFieldType (otype);    //result.     Defaultexpression:= "; Default Value    //The dataset must be set, and the last line is not easy to errorResult. dataset:=D;End;//returns the type string of a fieldFunction getfieldtypestring (Ofield:tfield): String;    Var N:integer; s:string; Begin Result:="'; Try N:=ofield.size; s:="'; //        ifOfield isTstringfield ThenBeginifN>naccessstringmaxlength ThenS:='Memo'ElsebeginN:=naccessstringmaxlength; S:='VarChar ('+INTTOSTR (n) +')';          End; End ElseifOfield isTlargeintfield ThenS:='Int'ElseifOfield isTsmallintfield ThenS:='Int'ElseifOfield isTintegerfield ThenS:='Int'ElseifOfield isTnumericfield ThenS:='Numeric (20,4)'ElseifOfield isTbooleanfield ThenS:='Bit'ElseifOfield isTblobfield ThenS:='Image'ElseifOfield isTdatetimefield ThenS:='DateTime'ElseifOfield isTbinaryfield ThenS:='Binary'ElseifOfield isTmemofield ThenS:='Memo'Else S:='VarChar (+)'; //result:=Trim (s); Except on E:exception DoRaise Exception.Create('[getfieldtypestring] Get field type description error'+# -+e.message); End; End;

Incoming data sets, generating SQL scripts for corresponding tables

Related Article

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.