python:sql建表语句转换为json

第一种sql格式:

  1 CREATE TABLE
  2     prpcitem_car
  3     (
  4         proposalno CHAR(22) NOT NULL,
  5         itemno DECIMAL(8,0) NOT NULL,
  6         riskcode CHAR(3) NOT NULL,
  7         insuredtypecode CHAR(2),
  8         carinsuredrelation CHAR(1),
  9         clausetype CHAR(3),
 10         licenseno VARCHAR(20),
 11         licensetype CHAR(3),
 12         licensecolorcode CHAR(2),
 13         nonlocalflag CHAR(1),
 14         licenseflag CHAR(1),
 15         vehicletype CHAR(4),
 16         carkindcode CHAR(3),
 17         hkflag CHAR(1),
 18         hklicenseno CHAR(20),
 19         engineno VARCHAR(30),
 20         vinno VARCHAR(18),
 21         frameno VARCHAR(30),
 22         runareacode CHAR(10),
 23         runareaname VARCHAR(60),
 24         runmiles DECIMAL(14,2),
 25         enrolldate DATE,
 26         useyears DECIMAL(15,255),
 27         vehiclecategory CHAR(3),
 28         columvehiclecategoryn_68 CHAR(3),
 29         modelcodealias VARCHAR(100),
 30         modelcode CHAR(14),
 31         brandname VARCHAR(200) NOT NULL,
 32         aliasname VARCHAR(100),
 33         countrynature CHAR(2),
 34         countrycode CHAR(3),
 35         usenaturecode CHAR(3),
 36         businessclasscode CHAR(1),
 37         seatcount DECIMAL(15,255),
 38         toncount DECIMAL(10,3),
 39         exhaustscale DECIMAL(8,4),
 40         cylindercount INTEGER,
 41         iscriterion INTEGER,
 42         lossratio CHAR(5),
 43         salesname VARCHAR(50),
 44         salesnumber VARCHAR(20),
 45         salesphone VARCHAR(15),
 46         carcounts INTEGER,
 47         isdropinvisitinsure INTEGER,
 48         carloteququality DECIMAL(14,2),
 49         colorcode CHAR(6),
 50         safedevice CHAR(30),
 51         coefficient1 DECIMAL(8,4),
 52         coefficient2 DECIMAL(8,4),
 53         coefficient3 DECIMAL(8,4),
 54         othernature VARCHAR(10),
 55         ratecode CHAR(8),
 56         rationname VARCHAR(60),
 57         makedate DATE,
 58         carusage VARCHAR(20),
 59         currency CHAR(3),
 60         purchaseprice DECIMAL(14,2),
 61         actualvalue DECIMAL(14,2),
 62         invoiceno CHAR(20),
 63         carloanflag CHAR(1),
 64         cardealercode CHAR(16),
 65         cardealername VARCHAR(120),
 66         remark VARCHAR(40),
 67         carid CHAR(10),
 68         versionno CHAR(4),
 69         monopolyflag CHAR(1),
 70         monopolycode VARCHAR(22),
 71         monopolyname VARCHAR(80),
 72         newcarflag CHAR(1),
 73         loanvehicleflag CHAR(1),
 74         transfervehicleflag CHAR(1),
 75         transferdate DATE,
 76         operationarea VARCHAR(5),
 77         inserttimeforhis DATETIME YEAR TO SECOND,
 78         operatetimeforhis DATETIME YEAR TO SECOND,
 79         startsitename VARCHAR(255),
 80         discounttype VARCHAR(255),
 81         drivertype VARCHAR(255),
 82         endsitename VARCHAR(255),
 83         carcusttype CHAR(2),
 84         nodamageyears VARCHAR(4),
 85         modeldemandno VARCHAR(50),
 86         energytype CHAR(1),
 87         fueltype CHAR(2),
 88         carprooftype CHAR(2),
 89         carproofno CHAR(50),
 90         carproofdate DATE,
 91         certificatedate DATE,
 92         isremote CHAR(1),
 93         fullendor CHAR(1),
 94         issilageharvester CHAR(1),
 95         flag CHAR(10),
 96         licenseno1 CHAR(10),
 97         licenseno2 VARCHAR(20),
 98         licenseno3 CHAR(10),
 99         vehiclebrand VARCHAR(20),
100         vehicletypedescription VARCHAR(5),
101         groupcode VARCHAR(50),
102         groupstartdate DATE,
103         groupenddate DATE,
104         PRIMARY KEY (proposalno, itemno) CONSTRAINT pk_citem_car,
105         FOREIGN KEY (proposalno) REFERENCES prpcmain (proposalno) CONSTRAINT fkb28a3afd9b27a158
106     );

对应python语句:

 1 import json
 2 def get_list(sqlLines):
 3     list =[]
 4     str =‘‘
 5     table_name=‘‘
 6     primary_key=‘‘
 7     foreign_key=‘‘
 8     for line in sqlLines:
 9         if ‘PRIMARY KEY‘ in line:
10             primary_key=line[line.find(‘(‘)+1:line.find(‘)‘)]
11         if ‘FOREIGN KEY‘ in line:
12             foreign_key=line[line.find(‘(‘)+1:line.find(‘)‘)]
13         str+=line
14     list.append(str)
15     list.append(primary_key)
16     list.append(foreign_key)
17     return list
18
19 def get_json(list):
20     str=list[0]
21     primary_key=list[1]
22     foreign_key=list[2]
23     table_name=str[str.find(‘CREATE TABLE‘)+len(‘CREATE TABLE‘):str.find(‘(‘)].strip()
24     str= str[str.find(‘(‘)+1:str.rfind(‘)‘)].strip()
25     str =str.lower()
26     str =str.replace(‘ not null‘,‘‘).strip()
27     if ‘primary key‘ in str:
28         str=str[0:str.find(‘primary key‘)]
29     if ‘foreign key‘ in str:
30         str=str[0:str.find(‘foreign key‘)]
31     if ‘datetime year to second‘ in str:
32         str= str.replace(‘datetime year to second‘,‘string‘)
33
34     str_lines =str.splitlines(False)
35     json_list=[]
36     for line in str_lines:
37         json_dict={}
38         lines =line.split()
39         if len(lines)>=2:
40             name=lines[0]
41             type=lines[1][0:len(lines[1])-1]
42             json_dict[‘name‘]=name
43
44             if ‘date‘ in type:
45                 type=‘string‘
46             if ‘decimal‘ in type:
47                  type=‘string‘
48             json_dict[‘type‘]=type
49             if name in primary_key:
50                 json_dict[‘primary‘]=1
51             if name in foreign_key:
52                 json_dict[‘foreign‘]=1
53         else:
54             continue
55         json_list.append(json_dict)
56     return json_list
57
58
59
60 def load():
61     sqlStr =open(‘C:/Users/Administrator.PC--20150529IGF/Desktop/prpcitem_car.txt‘)
62     sqlLines =sqlStr.readlines(100000)
63     sql_list =get_list(sqlLines)
64     str =get_json(sql_list)
65     jsonS =json.dumps(str)
66     print(jsonS)
67 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语句:

 1 import json
 2
 3 def cleanSql(lines):
 4     list=[]
 5     str =‘‘
 6     table_name=‘‘
 7     primary_key=‘‘
 8     for line in lines:
 9         if ‘CREATE TABLE‘ in line:
10             table_name=line[line.find(‘."‘)+1:line.rfind(‘"‘)]
11         if ‘PRIMARY KEY‘ in line:
12             primary_key=line[line.find(‘"‘)+1:line.rfind(‘"‘)]
13         str += line
14     str = str.lower()
15     if str.startswith(‘create table‘) is False:
16         str =str[str.index(‘create table‘):len(str)]
17     if ‘primary key‘ in str:
18         str=str[0:str.find(‘primary key‘)]
19     if ‘foreign key‘ in str:
20         str=str[0:str.find(‘foreign key‘)]
21     str= str[str.index(‘(‘)+1:]
22     if ‘not null‘ in str:
23         str=str.replace(‘not null‘,‘‘)
24     list.append(table_name)
25     list.append(primary_key.lower())
26     list.append(str)
27     return list
28
29 def changeType(type):
30     stype=type
31     if ‘character‘ in type:
32         stype =‘string‘
33     if ‘date‘ in type:
34         stype= ‘string‘
35     if ‘decimal‘ in type:
36         stype= ‘string‘
37     return stype
38
39 def get_json(list):
40    primary_key=list[1]
41    str =list[2]
42    lines= str.splitlines(False)
43    json_list=[]
44    for line in lines:
45        json_dict={}
46        name=line[line.find(‘"‘)+1:line.rfind(‘"‘)].strip()
47        type=line[line.rfind(‘"‘)+1:line.find(‘,‘)].strip()
48        type =changeType(type)
49        json_dict[‘type‘]=type
50        json_dict[‘name‘]=name
51        if primary_key==name:
52             json_dict[‘primary‘]=1
53        if len(name)==0:
54            continue
55        json_list.append(json_dict)
56    return json_list
57
58 def load():
59     schema=open(‘C:/Users/Administrator.PC--20150529IGF/Desktop/data/data/lis/llcase/full/20170401_09_37_01/schema.sql‘)
60     sqlStr =schema.readlines(100000)
61     list=cleanSql(sqlStr)
62
63     jsonStr =get_json(list)
64     jsonS =json.dumps(jsonStr)
65     print(jsonS)
66 load()

时间: 2024-11-08 17:08:00

python:sql建表语句转换为json的相关文章

三种常用的MySQL建表语句(转)

MySQL建表语句是最基础的SQL语句之一,下面就为您介绍最常用的三种MySQL建表语句,如果您对MySQL建表语句方面感兴趣的话,不妨一看. 1.最简单的: CREATE TABLE t1(    id int not null,    name char(20));2.带主键的: a:CREATE TABLE t1(    id int not null primary key,    name char(20));b:复合主键CREATE TABLE t1(    id int not n

Oracle根据Excel或者world数据字典自动生成建表语句和注释

1.创建生成建表语句的存储过程: create table t_OUTString ( Tname varchar2(50), OUTString clob, OUTSTRING2 clob, createddate date ); create or replace procedure Pro_ExToSql(SStrin in varchar2 ,TString in varchar2) is RString varchar2(30000); RowString varchar2(30000

06-使用框架提供的建表语句创建数据库表

4.    初始化表结构 4.1  使用activiti框架提供的建表语句 * 第一步:获得建表语句 *  第二步:创建一个数据库 * 第三步:进入当前数据库中 4.2  使用activiti框架的自动建表功能  类似于我们学过的hibernate,它可以自动建表 hbm2ddl.auto = update

mysql学习——列类型,建表语句和增删改查

建表语句 create table 表名( 列名称 列类型 [列属性] [默认值], ... )engine 引擎名 charset 字符集: mysql学习--列类型,建表语句和增删改查

网易视频云技术分享:HBase - 建表语句解析

网易视频云的技术专家给大家分享一篇技术性文章:HBase - 建表语句解析. 像所有其他数据库一样,HBase也有表的概念,有表的地方就有建表语句,而且建表语句还很大程度上决定了这张表的存储形式.读写性能.比如我们熟悉的MySQL,建表语句中数据类型决定了数据的存储形式,主键.索引则很大程度上影响着数据的读写性能.虽然HBase没有主键.索引这些概念,但在HBase的世界里,有些东西和它们一样重要! 废话不说,直接奉上一条HBase建表语句,来为各位看官分解剖析: create 'NewsCli

Mysql DBA 高级运维学习笔记-mysql建表语句及表知识

9.9 表操作 9.9.1以默认字符集建库 以默认格式的为例,指定字符集建库 [email protected] 07:0205->create database wwn; Query OK, 1 row affected (0.00 sec) [email protected] 07:0339->SHOW CREATE DATABASE wwn\G; *************************** 1. row *************************** Database

oracle导出表的建表语句拼接SQL

前段时间有个需求需要导出数据库的500张表结构,使用PLSQLDEV工具也可以导出建表语句,但是需要手动一个表一个表选,非常费劲.就写了个拼接sql. select 'select dbms_metadata.get_ddl(''' ||segment_type|| ''',''' ||segment_name|| ''',''' || owner || ''') from dual;' from dba_segments where segment_name='ALE_CRITICALDEGR

MySQL 建表语句(参考)

该建表sql语句在日常学习,工作中用于参考使用 CREATE TABLE classes( id int auto_increment primary key, classname char(30) not null )engine=innodb charset=utf8; CREATE TABLE students( id int auto_increment primary key, name char(24) not null, class_id int , unique uq1 (nam

Oracle --> Vertica 建表语句修改

需求:在Vertica数据库上建表,表结构来源于原Oracle数据库,故需要转换成Vertica数据库库表结构. 实际转换操作需要评估源库用到的所有数据类型和数据本身特性. 下面是总结的某场景下的替换规则,仅供参考. 1.时间类型: DATE   =>   TIMESTAMP 2.数字类型:NUMBER(   =>   NUMERIC( NUMBER   =>   NUMERIC(38,0) INTEGER   =>   NUMERIC(38,0) 3.字符类型: VARCHAR2