#2
wengjl2024-10-25 13:18
|
废话不说,奉上代码吧~
程序代码:
*********************mysqlgen.prg***********************
PARAMETERS calias
IF PARAMETERS()=0
calias=GETFILE([dbf])
IF !EMPTY(calias)
cmd=[use ]+[']+calias+[']
&cmd
ELSE
RETURN .f.
ENDIF
ENDIF
IF JUSTFNAME(calias)!=ALIAS()
cmd=[USE ]+[']+calias +['] +[ IN 0]
&cmd
SELECT &calias
ENDIF
COPY STRUCTURE EXTENDED TO dbfstruct
fname=ALIAS()
********************确定包含索引的field名******************
*************以确定是否有PRIMARY KEY | UNIQUE *************
ncount=TAGCOUNT()
cunique=[]
DO CASE
CASE ncount=1
cprimarykey=KEY(CDX(1),1)
CASE ncount>1
cprimarykey=KEY(CDX(1),1)
FOR i =2 TO ncount
cnuique=cunique+[ ]+KEY(CDX(1),i)
ENDFOR
OTHERWISE
cprimarykey=[]
ENDCASE
USE IN SELECT(0) &&可以关闭calias了
USE dbfstruct
*!*BROWSE
csql=[create TABLE ] +fname+CHR(13)
n=RECCOUNT()
DIMENSION field_definition[n]
STORE [] TO field_definition
csql=csql+CHR(9)+[(]
FOR i=1 TO n
GOTO i
DO CASE &&确定type定义脚本
CASE field_type=[I] && 整数
ctype=[INT ]
CASE field_type=[N] AND field_dec=0 && 整数
ctype=[INT]+[(]+transf(field_len)+[) ]
CASE field_type=[N] AND field_dec>0 && 小数
ctype=[DECIMAL(]+transf(field_len)+[,]+;
TRANSFORM(field_dec)+[) ]
CASE field_type=[B] &&双精度
ctype=DOUBLE(]+transf(field_len)+;
[,]+TRANSFORM(field_dec)+[) ]
CASE field_type=[F] &&浮点型
ctype=[FLOAT(]+transf(field_len)+;
[,]+TRANSFORM(field_dec)+[) ]
CASE field_type=[C] &&定长字符
ctype=[CHAR(]+transf(field_len)+[) ]
CASE INLIST(field_type,[V],[M]) &&变长字符串
ctype=[VARCHAR(1024)]
CASE INLIST(field_type,[Q],[G],[W]) &&二进制变长大字符串
ctype=[MEDIUMBLOB ]
CASE field_type=[D] &&日期
ctype=[DATE ]
CASE field_type=[T] &&日期时间
ctype=[DATETIME ]
CASE field_type=[Y] &&货币型
ctype=[DECIMAL(8,4) ]
CASE field_type=[L] &&逻辑型
ctype=[BOOL ]
ENDCASE
DO CASE &&确定索引脚本
CASE !EMPTY(cprimarykey) AND ;
ALLTRIM(UPPER(field_name))=UPPER(cprimarykey)
cindexscript=[PRIMARY KEY ]
CASE !EMPTY(cunique) AND;
(ALLTRIM(UPPER(field_name)) $ UPPER(cunique))
cindexscript=[INDEX INDEX_]+ALLTRIM(FIELD_NAME)+[ ]
OTHERWISE
cindexscript=[]
ENDCASE
***********这里放入每个field的定义*************
*!*create_difinition[1]=
*!*create_difinition[2]=
*!*...
*!*create_difinition[n]=
field_definition[i]=allt(field_name)+[ ]+ctype+;
[ ]+iif(field_null=.t.,[NULL ],[NOT NULL ]);
+iif(field_step>0,[AUTOINC NEXTVALUE ];
+transf(field_next)+[ STEP ]+;
transf(field_step),[])+iif(empty(field_defa),[],;
[DEFAULT ]+ICASE(ALLTRIM(FIELD_DEFA)=[.T.],;
[TRUE],ALLTRIM(FIELD_DEFA)=[.F.],[FALSE],;
allt(field_defa)))+[ ]+cindexscript+;
IIF(i=n,[)],[,]+CHR(13)+CHR(9))
csql=csql+field_definition[i]
***********************************************
ENDFOR
*!* 方便查看生成的sql脚本,完成后注释掉这两句就可以了
STRTOFILE(csql,"csql.txt")
MODIFY FILE csql.txt
*!* *****************************
文章连接:https://mp.weixin.