python:sql建表语句转换为json
第一种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()
python:sql建表语句转换为json的更多相关文章
- 借助python工具从word文件中抽取相关表的定义,最后组装建表语句-非常好
借助python工具从word文件中抽取表的定义,最后组装建表语句-非常好 --如有转载请以超链接的方式注明原文章出处,谢谢大家.请尊重每一位乐于分享的原创者 1.python脚本 ## -*- co ...
- Python导出MySQL数据库中表的建表语句到文件
为了做数据对象的版本控制,需要将MySQL数据库中的表结构导出成文件进行版本化管理,试写了一下,可以完整导出数据库中的表结构信息 # -*- coding: utf-8 -*- import os i ...
- MySQL建表语句生成Golang代码
1. 背景 对于后台开发新的需求时,一般会先进行各种表的设计,写各个表的建表语句 然后根据建立的表,写对应的model代码.基础的增删改查代码(基础的增删改查服务可以划入DAO(Data Access ...
- 三种常用的MySQL建表语句(转)
MySQL建表语句是最基础的SQL语句之一,下面就为您介绍最常用的三种MySQL建表语句,如果您对MySQL建表语句方面感兴趣的话,不妨一看. 1.最简单的: CREATE TABLE t1( ...
- powerDesigner的建表语句默认设置为去掉双引号
powerDesigner的建表语句默认设置为去掉双引号,依次执行如下操作: Database ------>> Edit Current DBMS ------>> Scri ...
- mysql和oracle建表语句以及数据类型的区别
1.mysql和oracle建表语句的区别 mysql DROP TABLE IF EXISTS `order`;CREATE TABLE `order` ( `id` int(11) NOT NU ...
- sqlite建表语句(特别是外键问题)
原创 sqlite建表语句(特别是外键问题) 下面图表示两个表关系: //表1User_invitecreate table User_invite(Invite_id INTEGER PRIMAR ...
- ORACLE 建表语句(表名及字段名大写)
ORACLE建表时如果表名或者字段名存在大小写同时存在的情况下,默认为区分大小写,此时在select/updata等操作时需要在表名或者字段名上添加双引号,否则会报"视图不存在"的 ...
- sqlserver 建表语句,获取建表语句的存储过程,包括排序规则,索引,字段说明,支持同时生成多个表
先创建一个分割表名的分割函数 --表值函数用以截取字符串 --如果为其添加一列主键id,则其顺序就会固定了 create FUNCTION [Split](@text NVARCHAR(max)) ) ...
随机推荐
- 精确获取对象的类型:Object.prototype.toString()
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
- CSS的常用属性(三)
静态定位 position: static (默认) 标准流 绝对定位 position: absolute 特点: 元素使用绝对定位之后,不占据原来的位置(脱标) 元素使用绝对定位,位置是从浏览器出 ...
- python--3、 可迭代对象、迭代器、生成器
可迭代对象 iterable 可直接作用于for循环的对象统称为可迭代对象. 有 list. dict.tuple.set.str等数据类型,还有 generator(包括生成器和带yield的gen ...
- 安卓通过UDP协议传输数据,中文乱码的问题
公司最近需要往智能家居方面发展,需要用到UDP协议传输数据,在网上找到了一些资料,但是发现传输中文的时候有乱码的现象,经过我多番捣鼓,终于解决了这个问题,下面贴上关键代码 客户端: public cl ...
- JWPL工具处理维基百科wikipedia数据用于NLP
JWPL处理维基百科数据用于NLP 处理zhwiki JWPL是一个Wikipedia处理工具,主要功能是将Wikipedia dump的文件经过处理.优化导入mysql数据库,用于NLP过程.以下以 ...
- ANN:DNN结构演进History—RNN
前言: CNN在图像处理领域的极大成功源于CNN的二维递进映射结构,通过训练多层卷积核来进行特征提取函数训练,在二维图像的稀疏表达和语义关联分析方面有天生的结构优势.而涉及时序问题的逻辑序列分析-边长 ...
- 【sqli-labs】 less28a GET- Blind based -All you Union&Select Belong to us -String -Single quote-parenthesis(GET型基于盲注的去除了Union和Select的单引号带括号字符型注入)
和less28没什么区别,直接上个payload吧 http://192.168.136.128/sqli-labs-master/Less-28a/?id=0')%a0uNion%a0sElect% ...
- mac系统下安装、启动、停止mongodb
mongodb是非关系型数据库,mysquel是关系型数据库,前者没有数据表这个说法,后者有 一. 下载nodejs,安装,一直到 node -v显示版本号,表示安装成功. 二. 本文主要讲解,安装包 ...
- Js 删除前弹出确认框
<td align="center" valign="middle" class="black3"> <c:if test ...
- javaee utf-8文件的转换
package Zy; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.Fil ...