python:sql建表语句转换为json
阅读原文时间:2023年07月11日阅读:1

第一种sql格式:

CREATE TABLE
prpcitem_car
(
proposalno CHAR(22) NOT NULL,
itemno DECIMAL(8,0) NOT NULL,
riskcode CHAR(3) NOT NULL,
insuredtypecode CHAR(2),
carinsuredrelation CHAR(1),
clausetype CHAR(3),
licenseno VARCHAR(20),
licensetype CHAR(3),
licensecolorcode CHAR(2),
nonlocalflag CHAR(1),
licenseflag CHAR(1),
vehicletype CHAR(4),
carkindcode CHAR(3),
hkflag CHAR(1),
hklicenseno CHAR(20),
engineno VARCHAR(30),
vinno VARCHAR(18),
frameno VARCHAR(30),
runareacode CHAR(10),
runareaname VARCHAR(60),
runmiles DECIMAL(14,2),
enrolldate DATE,
useyears DECIMAL(15,255),
vehiclecategory CHAR(3),
columvehiclecategoryn_68 CHAR(3),
modelcodealias VARCHAR(100),
modelcode CHAR(14),
brandname VARCHAR(200) NOT NULL,
aliasname VARCHAR(100),
countrynature CHAR(2),
countrycode CHAR(3),
usenaturecode CHAR(3),
businessclasscode CHAR(1),
seatcount DECIMAL(15,255),
toncount DECIMAL(10,3),
exhaustscale DECIMAL(8,4),
cylindercount INTEGER,
iscriterion INTEGER,
lossratio CHAR(5),
salesname VARCHAR(50),
salesnumber VARCHAR(20),
salesphone VARCHAR(15),
carcounts INTEGER,
isdropinvisitinsure INTEGER,
carloteququality DECIMAL(14,2),
colorcode CHAR(6),
safedevice CHAR(30),
coefficient1 DECIMAL(8,4),
coefficient2 DECIMAL(8,4),
coefficient3 DECIMAL(8,4),
othernature VARCHAR(10),
ratecode CHAR(8),
rationname VARCHAR(60),
makedate DATE,
carusage VARCHAR(20),
currency CHAR(3),
purchaseprice DECIMAL(14,2),
actualvalue DECIMAL(14,2),
invoiceno CHAR(20),
carloanflag CHAR(1),
cardealercode CHAR(16),
cardealername VARCHAR(120),
remark VARCHAR(40),
carid CHAR(10),
versionno CHAR(4),
monopolyflag CHAR(1),
monopolycode VARCHAR(22),
monopolyname VARCHAR(80),
newcarflag CHAR(1),
loanvehicleflag CHAR(1),
transfervehicleflag CHAR(1),
transferdate DATE,
operationarea VARCHAR(5),
inserttimeforhis DATETIME YEAR TO SECOND,
operatetimeforhis DATETIME YEAR TO SECOND,
startsitename VARCHAR(255),
discounttype VARCHAR(255),
drivertype VARCHAR(255),
endsitename VARCHAR(255),
carcusttype CHAR(2),
nodamageyears VARCHAR(4),
modeldemandno VARCHAR(50),
energytype CHAR(1),
fueltype CHAR(2),
carprooftype CHAR(2),
carproofno CHAR(50),
carproofdate DATE,
certificatedate DATE,
isremote CHAR(1),
fullendor CHAR(1),
issilageharvester CHAR(1),
flag CHAR(10),
licenseno1 CHAR(10),
licenseno2 VARCHAR(20),
licenseno3 CHAR(10),
vehiclebrand VARCHAR(20),
vehicletypedescription VARCHAR(5),
groupcode VARCHAR(50),
groupstartdate DATE,
groupenddate DATE,
PRIMARY KEY (proposalno, itemno) CONSTRAINT pk_citem_car,
FOREIGN KEY (proposalno) REFERENCES prpcmain (proposalno) CONSTRAINT fkb28a3afd9b27a158
);

对应python语句:

import json
def get_list(sqlLines):
list =[]
str =''
table_name=''
primary_key=''
foreign_key=''
for line in sqlLines:
if 'PRIMARY KEY' in line:
primary_key=line[line.find('(')+1:line.find(')')]
if 'FOREIGN KEY' in line:
foreign_key=line[line.find('(')+1:line.find(')')]
str+=line
list.append(str)
list.append(primary_key)
list.append(foreign_key)
return list

def get_json(list):
str=list[0]
primary_key=list[1]
foreign_key=list[2]
table_name=str[str.find('CREATE TABLE')+len('CREATE TABLE'):str.find('(')].strip()
str= str[str.find('(')+1:str.rfind(')')].strip()
str =str.lower()
str =str.replace(' not null','').strip()
if 'primary key' in str:
str=str[0:str.find('primary key')]
if 'foreign key' in str:
str=str[0:str.find('foreign key')]
if 'datetime year to second' in str:
str= str.replace('datetime year to second','string')

 str\_lines =str.splitlines(False)  
 json\_list=\[\]  
 for line in str\_lines:  
     json\_dict={}  
     lines =line.split()  
     if len(lines)>=2:  
         name=lines\[0\]  
         type=lines\[1\]\[0:len(lines\[1\])-1\]  
         json\_dict\['name'\]=name

         if 'date' in type:  
             type='string'  
         if 'decimal' in type:  
              type='string'  
         json\_dict\['type'\]=type  
         if name in primary\_key:  
             json\_dict\['primary'\]=1  
         if name in foreign\_key:  
             json\_dict\['foreign'\]=1  
     else:  
         continue  
     json\_list.append(json\_dict)  
 return json\_list

def load():
sqlStr =open('C:/Users/Administrator.PC--20150529IGF/Desktop/prpcitem_car.txt')
sqlLines =sqlStr.readlines(100000)
sql_list =get_list(sqlLines)
str =get_json(sql_list)
jsonS =json.dumps(str)
print(jsonS)
load()

---------------------

第二种sql格式:

-- Start of generated script for 10.136.1.5-DB2-LIS (db2inst1)
-- Apr-10-2017 at 16:35:18

CREATE TABLE "DB2INST1"."LLCASE"
("CASENO" VARCHAR(20) NOT NULL,
"RGTNO" VARCHAR(20),
"RGTTYPE" VARCHAR(1) NOT NULL,
"RGTSTATE" VARCHAR(2) NOT NULL,
"CUSTOMERNO" VARCHAR(24) NOT NULL,
"CUSTOMERNAME" VARCHAR(120),
"ACCIDENTTYPE" VARCHAR(1),
"RECEIPTFLAG" VARCHAR(1),
"HOSPITALFLAG" VARCHAR(1),
"SURVEYFLAG" VARCHAR(1),
"RGTDATE" DATE,
"HANDLEDATE" DATE,
"CLAIMCALDATE" DATE,
"AFFIXGETDATE" DATE,
"FEEINPUTFLAG" VARCHAR(1),
"INHOSPITALDATE" DATE,
"OUTHOSPITALDATE" DATE,
"INVALIHOSDAYS" SMALLINT,
"INHOSPITALDAYS" SMALLINT,
"DIANOSEDATE" DATE,
"POSTALADDRESS" VARCHAR(300),
"PHONE" VARCHAR(20),
"ACCSTARTDATE" DATE,
"ACCIDENTDATE" DATE,
"ACCIDENTSITE" VARCHAR(60),
"DEATHDATE" DATE,
"CUSTSTATE" VARCHAR(2),
"ACCDENTDESC" VARCHAR(6000),
"CUSTBIRTHDAY" DATE,
"CUSTOMERSEX" VARCHAR(1),
"CUSTOMERAGE" DECIMAL(5, 0),
"IDTYPE" VARCHAR(1),
"IDNO" VARCHAR(20),
"HANDLER" VARCHAR(10),
"UWSTATE" VARCHAR(10),
"DEALER" VARCHAR(10),
"APPEALFLAG" VARCHAR(1),
"TOGETHERGET" VARCHAR(1),
"GRPDEALFLAG" VARCHAR(1),
"GETMODE" VARCHAR(2),
"GETINTV" SMALLINT,
"CALFLAG" VARCHAR(1),
"UWFLAG" VARCHAR(1),
"DECLINEFLAG" VARCHAR(1),
"ENDCASEFLAG" VARCHAR(1),
"ENDCASEDATE" DATE,
"MNGCOM" VARCHAR(10) NOT NULL,
"OPERATOR" VARCHAR(10) NOT NULL,
"MAKEDATE" DATE NOT NULL,
"MAKETIME" VARCHAR(8) NOT NULL,
"MODIFYDATE" DATE NOT NULL,
"MODIFYTIME" VARCHAR(8) NOT NULL,
"BANKCODE" VARCHAR(10),
"BANKACCNO" VARCHAR(40),
"ACCNAME" VARCHAR(120),
"CASEGETMODE" VARCHAR(2),
"ACCMODIFYREASON" VARCHAR(400),
"CASENODATE" DATE,
"CASEPROP" CHARACTER(2),
"REMARK" VARCHAR(2000),
"CANCLEREASON" CHARACTER(2),
"CANCLEREMARK" VARCHAR(500),
"CANCLER" CHARACTER(10),
"CANCLEDATE" DATE,
"RIGISTER" CHARACTER(10),
"CLAIMER" CHARACTER(10),
"UWER" CHARACTER(10),
"UWDATE" DATE,
"SIGNER" CHARACTER(10),
"SIGNERDATE" DATE,
"RISKCODE" VARCHAR(10),
"OTHERIDTYPE" VARCHAR(2),
"OTHERIDNO" VARCHAR(50),
"GRPNO" VARCHAR(20),
"GRPNAME" VARCHAR(150),
"CONTDEALFLAG" VARCHAR(1),
"MOBILEPHONE" VARCHAR(15),
"PREPAIDFLAG" VARCHAR(1),
PRIMARY KEY("CASENO")
);

-- End of generated script for 10.136.1.5-DB2-LIS (db2inst1)

对应python语句:

import json

def cleanSql(lines):
list=[]
str =''
table_name=''
primary_key=''
for line in lines:
if 'CREATE TABLE' in line:
table_name=line[line.find('."')+1:line.rfind('"')]
if 'PRIMARY KEY' in line:
primary_key=line[line.find('"')+1:line.rfind('"')]
str += line
str = str.lower()
if str.startswith('create table') is False:
str =str[str.index('create table'):len(str)]
if 'primary key' in str:
str=str[0:str.find('primary key')]
if 'foreign key' in str:
str=str[0:str.find('foreign key')]
str= str[str.index('(')+1:]
if 'not null' in str:
str=str.replace('not null','')
list.append(table_name)
list.append(primary_key.lower())
list.append(str)
return list

def changeType(type):
stype=type
if 'character' in type:
stype ='string'
if 'date' in type:
stype= 'string'
if 'decimal' in type:
stype= 'string'
return stype

def get_json(list):
primary_key=list[1]
str =list[2]
lines= str.splitlines(False)
json_list=[]
for line in lines:
json_dict={}
name=line[line.find('"')+1:line.rfind('"')].strip()
type=line[line.rfind('"')+1:line.find(',')].strip()
type =changeType(type)
json_dict['type']=type
json_dict['name']=name
if primary_key==name:
json_dict['primary']=1
if len(name)==0:
continue
json_list.append(json_dict)
return json_list

def load():
schema=open('C:/Users/Administrator.PC--20150529IGF/Desktop/data/data/lis/llcase/full/20170401_09_37_01/schema.sql')
sqlStr =schema.readlines(100000)
list=cleanSql(sqlStr)

 jsonStr =get\_json(list)  
 jsonS =json.dumps(jsonStr)  
 print(jsonS)  

load()